Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use InCodeMappingProvider with swift #16

Closed
hopiaw opened this issue Feb 14, 2015 · 13 comments
Closed

How to use InCodeMappingProvider with swift #16

hopiaw opened this issue Feb 14, 2015 · 13 comments

Comments

@hopiaw
Copy link

hopiaw commented Feb 14, 2015

Hi,

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

  • (void)mapFromDictionaryKey:(NSString *)dictionaryKey toPropertyKey:(NSString *)propertyKey forClass:(Class)class withTransformer:(MappingTransformer)transformer;

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
Copy link
Owner

aryaxt commented Feb 14, 2015

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

@hopiaw
Copy link
Author

hopiaw commented Feb 14, 2015

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
Copy link
Owner

aryaxt commented Feb 14, 2015

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
Copy link
Author

hopiaw commented Feb 14, 2015

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
Copy link
Author

hopiaw commented Feb 14, 2015

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
Copy link
Owner

aryaxt commented Feb 14, 2015

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

@hopiaw
Copy link
Author

hopiaw commented Feb 14, 2015

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

@aryaxt
Copy link
Owner

aryaxt commented Feb 14, 2015

That's odd. Maybe change AnyObject to Any?

@hopiaw
Copy link
Author

hopiaw commented Feb 14, 2015

I tried yes, nope.

@hopiaw
Copy link
Author

hopiaw commented Feb 14, 2015

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
Copy link
Owner

aryaxt commented Feb 14, 2015

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

@aryaxt aryaxt closed this as completed Feb 14, 2015
@Sajjon
Copy link

Sajjon commented May 14, 2015

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
Copy link
Owner

aryaxt commented May 14, 2015

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants