RealmSwift-JSON icon indicating copy to clipboard operation
RealmSwift-JSON copied to clipboard

Cast of aValue as? List fails

Open mendrala opened this issue 9 years ago • 0 comments

Hi,

Thanks a lot for this library. For the most part, it works great. However, it seems that with Swift 2.2., the following line in the createJSONDictionary function never evaluates to true for my model:

guard let aValue = value as? List else {continue}

The List property that never gets cast from aValue looks like this in my model:

let preferences = List<UserPreference>()

It seems that Swift won't cast a generic type to a non-generic type (or even to a generic type that matches the base type for a set of objects).

I am using this workaround:

if let list = value as? ListBase {

    for index in 0..<list._rlmArray.count  {

         if let object = (list._rlmArray[index] as AnyObject) as? Object { // This cast is also weird, but necessary to make things work
            array.append(object.createJSONDictionary())
         }
     }

    result[propertyName] = array
 }

Maybe you can figure out a more elegant way to do it. Again, thanks a lot for this library, it's very useful.

mendrala avatar Aug 19 '16 14:08 mendrala