gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:13:45 24 4
gpt4 key购买 nike

我有两个模型类 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com