Search code examples
swiftrealm

Issue in Realm List to [Model] in swift


I have two model classes AlertRSM and AlertRSMList

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"
    }
}

In my view controller I am getting data from Realm Database

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

Now I print the alertRSMList

print(alertRSMList)

the output is

[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) 

and print the AlertRSM array

print(alertRSMList[0].arrayOfAlertRSM)

The output is

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) 

Now the problem statement is if I want to print AlertRSM's each property, I am getting nil

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

Output is nil where as it contains value "External"

nil
(lldb)

If I loop through the AlertList

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

All prints nil

I am also getting warning on this line

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

The continue code snippet is

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)

So please help me to get the values of

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

Solution

  • Specifiers dynamicand @objc are required by realm for member fields. Does it help to add these fields to AlertRSM class?