将领域列表中的问题发送到swift中的[Model]

时间:2017-12-02 10:24:52

标签: 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"
    }
}

在我的视图控制器中,我从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)

输出为零,因为它包含值&#34;外部&#34;

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

1 个答案:

答案 0 :(得分:2)

对于成员字段,

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