Beautiful and subtile Core Data changes in Swift on

Beautiful and subtile Core Data changes in Swift on #Xcode6Beta7

Flowing like swift!
I didn’t expect another beta before iOS release next week. But surprise – just one more thing – a new beta!

Yesterday the beta popped up, and today I’m investigating the changes. As a former SQL developer I’m most interested in the Core Data and the display of data in UITableViews.

Optionals, optionals and optionals

Some early comments indicated that there was a lot of changes on optionals – again. People reported on twitter that they had huge amount on errors when opening their projects.

I must admit I don’t really know how to use optionals. I’m still learning Swift. But debugging and writing this is a good way to learn more.

What is an optional however? Here are two thing about optionals I have in my memory as I’m writing:

  • Optionals is either some value or nothing ie. nil.
  • Optionals is used to reduce the number of values containing nil and thus make your program more robust.

All the concern of optionals is because backward compatibility to iOS libraries, where you can fin nileverywhere. Apple have spent 3 betas taking away unneeded nil’s. For instance, the Core Data stack in their Master Detail template is way more elegant and easy to understand now.

As my projects is only tests and turorials, I decided to look at changes in the Master Detail Core Data template that Apple provides. Looking at the errors produced for the beta6 template on beta7, I was able to see the differences. I liked what I discovered.

Changes in the Core Data stack in Beta7

The changes, it was seven of them, one was in AppDelegate, and the rest in MasterViewController.

I’ve marked the change with adding ** before and after the change.

One time unwrapping with if. Five times unwrap with !. Mark a property optional two times with ?.

In AppDelegate.swift I had to explicit unwrap an optional:

lazy var managedObjectModel: NSManagedObjectModel = {

let modelURL = NSBundle.mainBundle().URLForResource("mdDbApp", withExtension: "momd»)**!**

And in MasterViewController.swift there was various changes to optionals. First by using `if let´:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
**if** let indexPath = self.tableView.indexPathForSelectedRow() {

Then by marking for optional and use if-else operators ??

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.fetchedResultsController.sections**?**.count** ?? 0**

Then unwrap:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionInfo = self.fetchedResultsController.sections**!**[section] as NSFetchedResultsSectionInfo

Make it optional and unwrap it:

func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
cell.textLabel**?**.text = object.valueForKey("timeStamp")**!**.description

Then unwrap:

var fetchedResultsController: NSFetchedResultsController {

let entity = NSEntityDescription.entityForName("Event", inManagedObjectContext: self.managedObjectContext**!**)

… and a bit further down – unwrap again:

let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext**!**, sectionNameKeyPath: nil, cacheName: "Master")

What I learned with this comparision

  • I know a lot more on optionals than before, and I can even use them in simple context.
  • I’ve learned the value of reducing the posibilities of nil
  • I can foresee a future with lot less optionals
  • I’m even more found of Swift
  • I’ll try to only use libraries written in Swift (that’s impossible for Core Data, but soon…)
  • Even an old SQL guy can have something to say on this shiny new language

Comments are closed.