Lenx van Zyl Lenx van Zyl -4 years ago 100
Swift Question

Mapping to Realm Result to NSDictionary Error

I have been using this extension to successfully map my Realm Results to NSDictionary:

extension Object {
func toDictionary() -> NSDictionary {
let properties = self.objectSchema.properties.map { $0.name }
let dictionary = self.dictionaryWithValuesForKeys(properties)

let mutabledic = NSMutableDictionary()

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
mutabledic.setObject(objects, forKey: prop.name)
return mutabledic

But I am now trying to map :

let allObjectLists = realm.objects(UseItemList.self)
let firstObject = allObjectLists[0].valueForKey("useItems")
let toDict = firstObject?.toDictionary() //error here

How do I fix this, there must be a way to map
to a Dictionary

Here is the Error I get:

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'

And here is my
Object class:

class UseItemList: Object {
dynamic var dateCreated = NSDate()
dynamic var locationUnique = Int()
dynamic var MainActivityReference1 = ""
dynamic var MainActivityReference2 = ""
let useItems = List<UseItem>()

Answer Source

You're not accessing an Object there.

That line of code let firstObject = allObjectLists[0].valueForKey("useItems") is pulling out the useItems object, which is a List object. This is why it's reporting there's no method named toDictionary() available for it.

If you're trying to get the first object in useItems to generate a dictionary off that, it should be:

let allObjectLists = realm.objects(UseItemList.self) // Get all 'UseItemList' objects from Realm as a `Results` object
let firstObjectInList  = allObjectLists.first! // Get the first UseItemList object from the 'Results' object
let useItemsInFirstObject = firstObjectInList.useItems // Access the 'useItems' List object in the first object
let firstUseItem = useItems.first! // Access the first item from the 'useItems' List object
let toDict = firstItem.toDictionary() // Convert the first item into an array

Obviously you can condense this down into one line of code, but you need to make sure you're accessing all of the elements in the right order or else you won't get to a proper Object at the end. :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download