Closed hopiaw closed 9 years ago
mappingProvider.mapFromDictionaryKey("description", toPropertyKey:"fullDescription", forClass:User..self)
Yes but what about the value for the parameter withTransformer? I'm using a closure but it does not works. Thanks for the help.
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"
}
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"
})
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.
So do the first 2 examples still not work?
Last example is missing in
First 2 are working, and yes I saw that in missing for the third one. Still not working.
That's odd. Maybe change AnyObject to Any?
I tried yes, nope.
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.
cool, good catch. Hopefully Xcode 6.3 is gonna be more informative about errors :)
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! :)
@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
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.