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

NSDate Default Mapping #44

Open
abduranjum opened this issue Mar 14, 2016 · 3 comments
Open

NSDate Default Mapping #44

abduranjum opened this issue Mar 14, 2016 · 3 comments

Comments

@abduranjum
Copy link

My understanding from the documentation of OCMapper is that if nothing is set, it should automatically map and reverse map an NSDate. But unfortunately, it doesn't. When it is mapping from object to dictionary, it uses the "description" property of NSDate to get the string. But when it converts from dictionary to object, it is unable to convert it back to NSDate, as it doesn't have a matching dateFormatter. The workaround is to set the defaultDateFormatter to a dateFormatter independent of the locale of the device.

@aryaxt
Copy link
Owner

aryaxt commented Mar 14, 2016

The logic for converting from object to dictionary is a little different.
For dictionary to object we can analyze the string to decide what format its using, but for object to dictionary we don't know what the expected format in the dictionary should be. Here is the logic.

https://github.com/aryaxt/OCMapper/blob/master/OCMapper/Source/ObjectMapper.m#L183

It attempts to use a given date formatter for the specific class, if it doesn't exist it attempts to use a default date formatter, otherwise it uses the description.

You either need to give it a default date formatter or a class specific date formatter

@abduranjum
Copy link
Author

That should be stated in the documentation, that it is necessary to set defaultDateFormatter for NSDate to dictionary conversions. And if description was used, it should be handled in the dictionary to NSDate conversion. It's like the mapper cannot read the value it has written itself.

@aryaxt
Copy link
Owner

aryaxt commented Mar 14, 2016

Good point, will add this as enhancement

"Add a new dateformatter to defaultDateFormatters that can handle conversion of NSDate.description"

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

No branches or pull requests

2 participants