iOS Archives

These posts are syndicated from my iOS Dev Diary over at pinkstone.co.uk.

The format is slightly different with more code snippets and less explanations. I had originally designed it as a notebook in iOS Development – now you can follow along if you’re interested.

Hilarious Xcode Error Message

Xcode_iconI’ve been working on an iOS App recently which deals with several date methods. Usually when something goes wrong Xcode displays very dry messages such as “Array out of bounds” or something rather unhelpful (like the complete stack output with no clue as to what actually went wrong).

I was accidentally passing nil into an NSDateComponents method – and instead of crashing (which is what I would have expected), Xcode displayed this super funny message in the log console:

[__NSCFCalendar components:fromDate:toDate:options:]:

toDate cannot be nil

I mean really, what do you think that operation is supposed to mean with a nil toDate?

An exception has been avoided for now. A few of these errors are going to be reported with this complaint, then further violations will simply silently do whatever random thing results from the nil.

It made me smile 🙂

Read more

How to determine how many days / months / years have passed between two NSDate objects

Imagine you had two NSDate objects and you’d like to find out the time interval between those dates. NSDate objects alone won’t help us out there unless we do some serious NSDateFormatting and hair pulling. Lucky for us there are a few other classes available that will help us do this, namely NSCalendar and NSDateComponents. […]

How to pop a UINavigationController in code

In UINavigationController speak, we “push” new controllers onto the stack (go forward), and we “pop” them off the stack (go back). The navigation controller handles all this for us. If you want to go back exactly one view controller, here’s how you can do it programmatically: // pop back to previous controller NSArray *myControllers = […]

How to change the back button text on a UINavigationController

In Your Storyboard I didn’t know the storyboard could do this, but it can: Select the Navigation Bar in question, then check the Attributes Inspector: In Code To set the title in code, you need to address the self.navigationItem.backBarButton property of your view controller and pass it a new UIBarButtonItem. Here’s one way of doing […]

How to “quick save” in Core Data

The easiest way to access a readily available save method is by importing AppDelegate into the class that wants to execute the save. Then we call a method in AppDelegate: AppDelegate *myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate]; [myAppDelegate saveContext]; No reference to fetchedResultsController or managedObjectContext or any complex &error statements.

How to define a Protocol

Protocols are a great way to delegate responsibility from one class to another. Consider a view controller that brings up another view controller via a modal segue. Let’s call them ViewController and DetailViewController. When it comes to dismissing that DetailViewController, he could do it himself via [self dismissViewControllerAnimated:YES completion:nil]; However, the first ViewController wouldn’t be […]