Mantle - Convert a nested model to Swift

I have a lot of problems with de-serializing and serializing a nested model in Swift using Mantle. I believe that everything is correctly configured, but I can’t even get past compilation errors. To give some perspective, I successfully converted classes that don't have nested model objects. Here is my class:

class TheClass : MTLModel, MTLJSONSerializing
{
    var person:Person?

    static func JSONKeyPathsByPropertyKey() -> [NSObject : AnyObject]!
    {
        return ["person" : "person"]
    }

    static func personJSONTransformer() -> NSValueTransformer!
    {
        return MTLValueTransformer.reversibleTransformerWithForwardBlock(
        { (person:[NSObject : AnyObject]!) -> AnyObject! in
            do
            {
                return MTLJSONAdapter.modelOfClass(Person.self, fromJSONDictionary: person)
            }
            catch
            {
                return Person()
            }
        },
        reverseBlock:
        { (person:Person) -> AnyObject! in
            return MTLJSONAdapter.JSONDictionaryFromModel(person)
        })
    }
}

This code will not compile, and I cannot compile it. Here is the error message I get:

Cannot convert return expression of type 'AnyObject!' to expected return type 'Person'

I tried changing the return type of the inverse block to Personand Person!, but I am getting the same error message. I struggled with this for quite some time and couldn't find a working example in Swift, so any help would be greatly appreciated.

+4
1

, ! - , JSON. :

class TheClass : MTLModel, MTLJSONSerializing
{
    var person:Person?

    static func JSONKeyPathsByPropertyKey() -> [NSObject : AnyObject]!
    {
        return ["person" : "person"]
    }

    static func personJSONTransformer() -> NSValueTransformer!
    {
        return MTLJSONAdapter.dictionaryTransformerWithModelClass(Person.self)
    }
}

:

class TheClass : MTLModel, MTLJSONSerializing
{
    var person:[Person]?

    static func JSONKeyPathsByPropertyKey() -> [NSObject : AnyObject]!
    {
        return ["person" : "person"]
    }

    static func personJSONTransformer() -> NSValueTransformer!
    {
        return MTLJSONAdapter.arrayTransformerWithModelClass(Person)
    }
}

, !

+4

All Articles