我一直在使用這個擴展我的境界結果成功地映射到的NSDictionary:映射到域結果的NSDictionary錯誤
extension Object {
func toDictionary() -> NSDictionary {
let properties = self.objectSchema.properties.map { $0.name }
let dictionary = self.dictionaryWithValuesForKeys(properties)
let mutabledic = NSMutableDictionary()
mutabledic.setValuesForKeysWithDictionary(dictionary)
for prop in self.objectSchema.properties as [Property]! {
// find lists
if let nestedObject = self[prop.name] as? Object {
mutabledic.setValue(nestedObject.toDictionary(), forKey: prop.name)
} else if let nestedListObject = self[prop.name] as? ListBase {
var objects = [AnyObject]()
for index in 0..<nestedListObject._rlmArray.count {
let object = nestedListObject._rlmArray[index] as AnyObject
objects.append(object.toDictionary())
}
mutabledic.setObject(objects, forKey: prop.name)
}
}
return mutabledic
}
}
但現在我試圖映射:
let allObjectLists = realm.objects(UseItemList.self)
let firstObject = allObjectLists[0].valueForKey("useItems")
let toDict = firstObject?.toDictionary() //error here
我如何修復此問題,必須有方法將allObjectLists[0].valueForKey("useItems")
映射到字典
以下是我收到的錯誤:
2016-11-10 11:45:09.056 CPS Stocker[6187:167500] -[_TtGC10RealmSwift4ListC11CPS_Stocker7UseItem_ toDictionary]: unrecognized selector sent to instance 0x7fa7f3a49650
2016-11-10 11:45:09.253 CPS Stocker[6187:167500] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtGC10RealmSwift4ListC11CPS_Stocker7UseItem_ toDictionary]: unrecognized selector sent to instance 0x7fa7f3a49650'
這裏是我的UseItemList
對象類:
class UseItemList: Object {
dynamic var dateCreated = NSDate()
dynamic var locationUnique = Int()
dynamic var MainActivityReference1 = ""
dynamic var MainActivityReference2 = ""
let useItems = List<UseItem>()
}
你得到什麼錯誤? UseItemList的定義是什麼? – Michael
也請分享'UseItemList'類定義 – Dmitry