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

MissingMappingProvider is Swift usage #20

Closed victorjussiani closed 9 years ago

victorjussiani commented 9 years ago

Hello,

could you help me, when I try to run the following command

ObjectMapper.sharedInstance().objectFromSource(self.data, toInstanceOfClass: Message.self) as? Message 

it returns the following error:

2015-03-12 11:31:16.385 websocket[41545:3347379] *** Terminating app due to uncaught exception 'MissingMappingProvider', reason: 'Mapping provider is not set'

I'm forgetting to do something?

aryaxt commented 9 years ago

I'll get rid of that exception as mappingProvider is not always necessary Here is a fix, let me know if it resolves the issue.

ObjectMapper.sharedInstance().mappingProvider = InCodeMappingProvider()
victorjussiani commented 9 years ago

Thank you for the quick response!

When I added the MappingProvider he ask for InstanceProvider, so I added the following line and everything worked normally.

        ObjectMapper.sharedInstance().instanceProvider = ObjectInstanceProvider();

I would like to take to take a doubt, I have the following JSON:

{"event":  "connect", "data": {"soid": "_ w0_8000_0_ASfNBRdLv12uNUB9AABL", "appName": "app"}, "cid": 1}

As you can see I have a json within another json, the first layer of the JSON I deserialize an object, however the second JSON "data" I would like to deserializes it then because this can be different types, so I need keep it into a NSDictionary.

For this I created the following object to represent this json:

import Foundation

@objc public class SocketMessage: NSObject {

    var event: NSString ?;
    var cid: NSNumber ?;
    var data: NSDictionary? ;
 }

However when he deserialize the object to NSDictionary property is null, you could help me with this problem?

aryaxt commented 9 years ago

I think you are using the version in cocoapods which is old (Doesn't support swift). Use the version from github instead.

Swift uses module name as part of class names and therefore OCMapper should be in the same module as the models you are trying to map.

Add the source folder directly into your project. Also the line of code you have there is no longer needed (setting ObjectInstanceProvider), that's done by default in the latest version

Will fix it on the next version so that the module name can be passed to OCMapper, and will submit a new version to cocoapods

victorjussiani commented 9 years ago

I changed the version, however the error still persists, the property "data" is null;

aryaxt commented 9 years ago

aaah I see, it won't automatically map it as dictionary you need to write mapping for that

inCodeMappingProvider.mapFromDictionaryKey("data", toPropertyKey:"data", forClass: SocketMessage.self) {
      // don't try to map the node
      return $0
}
victorjussiani commented 9 years ago

I did the same as you said,

var inCodeMappingProvider: InCodeMappingProvider = InCodeMappingProvider();
inCodeMappingProvider.mapFromDictionaryKey("data", toPropertyKey:"data", forClass: SocketMessage.self);
ObjectMapper.sharedInstance().mappingProvider = inCodeMappingProvider;

However keeps coming null.

aryaxt commented 9 years ago

That's not the same, you are not using dataTransformers

inCodeMappingProvider.mapFromDictionaryKey("data", toPropertyKey:"data", forClass: SocketMessage.self) { return $0 }
victorjussiani commented 9 years ago

Sorry friend, I was making a mess, now worked perfectly.

Hopefully to update the version in CocoaPods.

Thank you very much

aryaxt commented 9 years ago

Great Will update the CocoaPods version soon