Thursday, December 17, 2015

Converting Date to a String or String to a Date in Swift

A very common use case many of you will come across is converting a NSDate object to a String object or vice versa. When I came across a use case where I had to convert a Date to String in my iOS app written in Swift I did an online search but it was hard to find a good example of the correct solution so I hope to provide an easy reference for anyone who is looking to implement the same.

Scenario 1: Converting Date to String

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "Y/M/d"
let duedateString = dateFormatter.stringFromDate(dueDatePicker.date)

Note the format "Y/M/d" used in the second line above is a Date Format value that defines how the date is represented in the String format. You can find a good resource here for the various values you can use to define the date format.

Also note that the dueDatePicker.date value used in the last line of the code above is referencing a Date Picker in the UI of my app, however, you can simply replace it with any Date object.

Let's say the user picked 12/25/2015 as the date, the value of duedateString at the end of the code's execution will be "2015/12/25" because of the format we used which says give me a string in Y/M/d format.

Scenario 2: Converting String to Date

This is even simpler and requires only 2 lines of code.

let dateFormatter = NSDateFormatter()
let dueDate = dateFormatter.dateFromString("12/17/2015")

Hope this helps!

Regards,

Tuesday, November 24, 2015

The model used to open the store is incompatible with the one used to create the store

If you are working on an iOS app and have used Core Data in your app you might suddenly come across an error with any of these messages: "The model used to open the store is incompatible with the one used to create the store," "Failed to initialize the application's saved data," or "There was an error creating or loading the application's saved data."

Don't fret, this simply means some of the data model for your Core Data has changed, it could be something as simple as adding a new attribute to an entity in Core Data, or modifying the data type of an attribute. Luckily the solution is quite simple in most cases - you can simply delete the app from your device or simulator and install/run the app again and it should work fine.

Note how I said the solution is simple for most cases; that's because it is not always practical to delete the app before installing the new version - either because you are testing upgrade functionality that you cannot test if you uninstall the app, or because you want to launch the app in the App Store and can't really expect your users to uninstall your app before installing an update. That's a sure way to have your app permanently uninstalled from the user's device! Unfortunately in this case modifying items in Core Data is not as easy; you'll have to migrate your data which entails writing some code to take care of it. I'll cover this in a future post but the solution above should work for over 90% of your needs while in development.

Cheers,
Paras Wadehra
Microsoft MVP
My WP Apps
http://twitter.com/ParasWadehra
FB.com/MSFTDev