aryaxt / OCMapper

Objective-C library to easily map NSDictionary to model objects, works perfectly with Alamofire. ObjectMapper works similar to GSON
MIT License
347 stars 45 forks source link

How to use InCodeMappingProvider with swift #16

Closed hopiaw closed 9 years ago

hopiaw commented 9 years ago

Hi,

I'm not sure this question is relevant but how do you use:

in swift?

I always got the following error: Argument withTransformer must precede argument forClass.

Could you post an example of this method used in Swift please.

Thanks.

aryaxt commented 9 years ago

mappingProvider.mapFromDictionaryKey("description", toPropertyKey:"fullDescription", forClass:User..self)

hopiaw commented 9 years ago

Yes but what about the value for the parameter withTransformer? I'm using a closure but it does not works. Thanks for the help.

aryaxt commented 9 years ago

Oops overlooked it. Don't have swift setup so gonna take a guess. Let me know if it doesn't work, and I'll setup swift and find out why it's not working.

mappingProvider.mapFromDictionaryKey("description", toPropertyKey:"fullDescription", forClass:User.self) { node, parent in 
   // node & parent are AnyObject to you need to cast them to appropriate type to use them (use if let)
   return "Something"
}
hopiaw commented 9 years ago

Humm... this works but when using directly the parameter withTransformer in 3rd example below, I'm not able to make it work. Weird.

The first 2 examples work, but not the third one. The one I was using of course:

    inCodeMappingProvider.mapFromDictionaryKey("key",
        toPropertyKey: "property",
        forClass: PatientRecord.self){
            node, parent in
            return "Something"
        }

    inCodeMappingProvider.mapFromDictionaryKey("key",
        toPropertyKey: "property",
        forClass: PatientRecord.self,
        withTransformer:{ s1, s2 in return s1})

    inCodeMappingProvider.mapFromDictionaryKey("key",
        toPropertyKey: "property",
        forClass: PatientRecord.self,
        withTransformer: { (s1:AnyObject, s2:AnyObject) -> AnyObject in
            return "Something"
    })
hopiaw commented 9 years ago

I always got the error: Argument withTransformer must precede argument forClass. with the third example above. It might be due to the bridge between Objective C and Swift. Idk...

If you found whats causing the issue, please let me know.

Thanks a lot for your help and librairie! It help the community a lot.

aryaxt commented 9 years ago

So do the first 2 examples still not work? Last example is missing in

hopiaw commented 9 years ago

First 2 are working, and yes I saw that in missing for the third one. Still not working.

aryaxt commented 9 years ago

That's odd. Maybe change AnyObject to Any?

hopiaw commented 9 years ago

I tried yes, nope.

hopiaw commented 9 years ago

Oh god! Of course... The signature of MappingTransformer use id. So parameters in the closure must be optional. Now it works:

    inCodeMappingProvider.mapFromDictionaryKey("key",
        toPropertyKey: "property",
        forClass: PatientRecord.self,
        withTransformer: { (s1:AnyObject?, s2:AnyObject?) -> AnyObject in
            return "Something"
    })

Again, swift error was a big help, haha!

Thanks again for your help.

aryaxt commented 9 years ago

cool, good catch. Hopefully Xcode 6.3 is gonna be more informative about errors :)

Sajjon commented 9 years ago

Hi! Just found this Github projects, it looks amazing! And I am thinking about replacing JSONJoy with it, since JSONJoy does not automatically match JSON values to Model properties.

Where can I put the inCodeMappingProvider.mapFromDictionaryKey code, when I want to use another name of my property than the JSON key?

I am using Swift, Alamofire and the Alamofire.Request extension

Can I add code for "re-mapping" of property-JSONkey names inside the init for each model?

Extremely thankful for any help!

Thanks! :)

aryaxt commented 9 years ago

@Sajjon Glad you found it helpful. I personally don't like adding parsing logic to models, I usually create a mapping manager file that has all my mapping, and I call it from appDelegate. Take a look at this class: https://github.com/aryaxt/OCMapper/blob/master/OCMapper/Sample/Service%20Layer/OCMapperConfig.m