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

Array of dates #27

Closed W2N-git closed 9 years ago

W2N-git commented 9 years ago

I need array on NSDate, like this

@objc public class User: NSObject { var dates: [NSDate]? }

And in JSON { "dates": [ "11/12/13", ] }

But providing property does not work.

let propertyProvider = InCodeMappingProvider() propertyProvider.mapFromDictionaryKey("dates", toPropertyKey: "dates", withObjectType: NSDate.self, forClass: User.self)

ObjectMapper still generates strings.

Should I use provider with transformer or there is other way to do what i want?

Works fine if I add to method

condition

else if ([source isKindOfClass:[NSString class]] && class == [NSDate class] ){ return [self dateFromString:source forProperty:nil andClass:NULL]; }

But it looks ugly)

aryaxt commented 9 years ago

That won't work, you need to write a transformer.

[mappingProvider mapFromDictionaryKey:@"dates" toPropertyKey:@"dates" forClass:[User class] withTransformer:^id(id currentNode, id parentNode) {
     NSArray *arrayOfDates = [NSMutableArray array];
     // Loop through currentNode (array of strings), convert them to NSDate and add them to arrayOfDates
     return arrayOfDates;
}];
mappingProvider.mapFromDictionaryKey("dates", toPropertyKey:"dates", forClass:User.self) { node, parent in 
   var dates = [NSDate]()
   if let arrayOfStrings = node as? [String] {
       // convert string to date and add to array
   }

   return dates
}