Global Constants

The code you wrote previously uses literal string values to identify the key fields used in NSUserDefaults. This can be a problem, because if they aren’t spelled the same everywhere you use them, you will get some very strange error messages. One solution to this problem is to create an object to hold global constants, and then define the string keys in that object. Here’s how to update the code to be more robust.

1. Select File > New > File, and select Objective-C class in the Cocoa Touch category. Click Next.

2. Name the class Constants and enter NSObject for Subclass Of. Click Next and make sure to save the file inside the project folder, and that the Target for MyContactList CoreData is checked.

3. Open the Constants.h file ...

Get Learning Mobile App Development: A Hands-on Guide to Building Apps with iOS and Android now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.