swift - 在 Realm List 中快速发布到 [Model]

标签 swift realm

我有两个模型类 AlertRSMAlertRSMList

class AlertRSM : Object{

    var alertType : String?
    var alertTypeValue : String?
    var period : String?
    var colorValue : String?
    var tableName : String?

    convenience init(dict:Dictionary<String,Any>) {
        self.init()
        if let _alertType = dict["AlertType"] as? String {
            alertType = String(_alertType)
        }
        if let _alertTypeValue = dict["AlertTypeValue"] as? Double {
            alertTypeValue = String(_alertTypeValue)
        }
        if let _period = dict["Period"] as? String {
            period = _period
        }
        if let _colorValue = dict["ColorValue"] as? String {
            switch _colorValue {
                case "G":
                    self.colorValue = "008000"
                case "R":
                    self.colorValue = "FF0000"
                case "Y":
                    self.colorValue = "FFFF00"
            default: break
            }
        }
        if let _tableName = dict["TableName"] as? String {
            tableName = _tableName
        }
    }

    static func modelArrayFromDictionaryArray(dictArray : [Dictionary<String,Any>]) -> [AlertRSM] {
        var array = [AlertRSM]()

        for item in dictArray {
            array.append(AlertRSM(dict: item))
        }
        return array
    }
}

class AlertRSMList: Object {
    dynamic var companyAlt_Key :String?
    dynamic var dbEntryDate :String?

    var arrayOfAlertRSM = List<AlertRSM>()

    convenience init(dict:[AlertRSM]) {
        self.init()
        for item in dict {
            arrayOfAlertRSM.append(item)
        }
        dbEntryDate = CommonMethods.getDateInString()
        companyAlt_Key = STATIC_STORAGE.Company_Alt_Key
    }

    override static func primaryKey() -> String? {
        return "companyAlt_Key"
    }
}

在我的 View Controller 中,我从 Realm 数据库获取数据

let _toDaysAlertRSM = realm.objects(AlertRSMList.self)
let alertRSMList : [AlertRSMList] = _toDaysAlertRSM.filter { alertRSM in
    return alertRSM.dbEntryDate == CommonMethods.getDateInString()
}

现在我打印 alertRSMList

print(alertRSMList)

输出是

[AlertRSMList {
companyAlt_Key = 3;
dbEntryDate = 02/12/2017;
arrayOfAlertRSM = RLMArray <0x6000000f1f80> (
    [0] AlertRSM {
        alertType = External;
        alertTypeValue = 14.77;
        period = M;
        colorValue = 008000;
        tableName = Alert;
    },
    [1] AlertRSM {
        alertType = External;
        alertTypeValue = 15.64;
        period = Q;
        colorValue = 008000;
        tableName = Alert;
    },
    [2] AlertRSM {
        alertType = Financial;
        alertTypeValue = 40.78;
        period = Q;
        colorValue = 008000;
        tableName = Alert;
    },
    [3] AlertRSM {
        alertType = Financial;
        alertTypeValue = 40.78;
        period = M;
        colorValue = 008000;
        tableName = Alert;
    },
    [4] AlertRSM {
        alertType = Financial;
        alertTypeValue = 40.78;
        period = W;
        colorValue = 008000;
        tableName = Alert;
    },
    [5] AlertRSM {
        alertType = External;
        alertTypeValue = 47.62;
        period = W;
        colorValue = FFFF00;
        tableName = Alert;
    },
    [6] AlertRSM {
        alertType = Statistical;
        alertTypeValue = 54.13;
        period = W;
        colorValue = FFFF00;
        tableName = Alert;
    },
    [7] AlertRSM {
        alertType = Statistical;
        alertTypeValue = 54.13;
        period = M;
        colorValue = FFFF00;
        tableName = Alert;
    },
    [8] AlertRSM {
        alertType = Statistical;
        alertTypeValue = 54.13;
        period = Q;
        colorValue = FFFF00;
        tableName = Alert;
    },
    [9] AlertRSM {
        alertType = NonFinancial;
        alertTypeValue = 78.76;
        period = W;
        colorValue = FF0000;
        tableName = Alert;
    },
    [10] AlertRSM {
        alertType = NonFinancial;
        alertTypeValue = 78.76;
        period = M;
        colorValue = FF0000;
        tableName = Alert;
    },
    [11] AlertRSM {
        alertType = NonFinancial;
        alertTypeValue = 78.76;
        period = Q;
        colorValue = FF0000;
        tableName = Alert;
    }
);
}]
(lldb) 

并打印AlertRSM数组

print(alertRSMList[0].arrayOfAlertRSM)

输出是

List<AlertRSM> <0x6080000f3280> (
[0] AlertRSM {
    alertType = External;
    alertTypeValue = 14.77;
    period = M;
    colorValue = 008000;
    tableName = Alert;
},
[1] AlertRSM {
    alertType = External;
    alertTypeValue = 15.64;
    period = Q;
    colorValue = 008000;
    tableName = Alert;
},
[2] AlertRSM {
    alertType = Financial;
    alertTypeValue = 40.78;
    period = Q;
    colorValue = 008000;
    tableName = Alert;
},
[3] AlertRSM {
    alertType = Financial;
    alertTypeValue = 40.78;
    period = M;
    colorValue = 008000;
    tableName = Alert;
},
[4] AlertRSM {
    alertType = Financial;
    alertTypeValue = 40.78;
    period = W;
    colorValue = 008000;
    tableName = Alert;
},
[5] AlertRSM {
    alertType = External;
    alertTypeValue = 47.62;
    period = W;
    colorValue = FFFF00;
    tableName = Alert;
},
[6] AlertRSM {
    alertType = Statistical;
    alertTypeValue = 54.13;
    period = W;
    colorValue = FFFF00;
    tableName = Alert;
},
[7] AlertRSM {
    alertType = Statistical;
    alertTypeValue = 54.13;
    period = M;
    colorValue = FFFF00;
    tableName = Alert;
},
[8] AlertRSM {
    alertType = Statistical;
    alertTypeValue = 54.13;
    period = Q;
    colorValue = FFFF00;
    tableName = Alert;
},
[9] AlertRSM {
    alertType = NonFinancial;
    alertTypeValue = 78.76;
    period = W;
    colorValue = FF0000;
    tableName = Alert;
},
[10] AlertRSM {
    alertType = NonFinancial;
    alertTypeValue = 78.76;
    period = M;
    colorValue = FF0000;
    tableName = Alert;
},
[11] AlertRSM {
    alertType = NonFinancial;
    alertTypeValue = 78.76;
    period = Q;
    colorValue = FF0000;
    tableName = Alert;
}
)
(lldb) 

现在的问题是,如果我想打印 AlertRSM 的每个属性,我将得到 nil

print(alertRSMList[0].arrayOfAlertRSM[0].alertType)

输出为零,因为它包含值“外部”

nil
(lldb)

如果我遍历 AlertList

for item in alertRSMList[0].arrayOfAlertRSM[0] {
    print(item.alertType)
    print(item.alertTypeValue)
    print(item.period)
    print(item.colorValue)
    print(item.tableName)
}

所有打印为零

我也在这一行收到警告

print(alertRSMList[0].arrayOfAlertRSM[0].alertType)
// Warning
// Expression implicitly coerced from 'String?'to Any

继续的代码片段是

let _toDaysAlertRSM = realm.objects(AlertRSMList.self)
let alertRSMList : [AlertRSMList] = _toDaysAlertRSM.filter { alertRSM in
    return alertRSM.dbEntryDate == CommonMethods.getDateInString()
}

print(alertRSMList)
print(alertRSMList[0].arrayOfAlertRSM)
print(alertRSMList[0].arrayOfAlertRSM[0].alertType)

所以请帮我获取值

alertRSMList[0].arrayOfAlertRSM[0].alertType//alertTypeValue//period

最佳答案

说明符 dynamic@objcrequired by realm对于成员字段。将这些字段添加到 AlertRSM 类是否有帮助?

关于swift - 在 Realm List 中快速发布到 [Model],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47606868/

相关文章:

swift - 如何使用 ICDeviceBrowser - 找不到相机

swift - 通过 NSAppleScript 编写 iTunes 脚本的沙盒权限

Android Realm 处理关系对象中的主键

ios - Realm 类属性不能标记为动态

swift - RealmSwift 错误 : "RLMException, reason: Collection was mutated while being enumerated."

android - 将预先存在的(未加密的) Realm 数据库迁移到新的加密 Realm 数据库

ios - 是否可以按 RLMArray 属性的计数对 RLMResults 进行排序?

swift - 关于比较和生成字符串

swift - 如何在 URL 中使用特殊字符\n

swift - Swift iOS 10 中的自定义键盘