iOS Development

Course No. 10-152-139

Assignment 25-1

My Contacts

  • Create a new Xcode command-line project. Name the project Assignment-25-1.
  • This project will consist of two classes.
    • MyContacts will contain an NSDictionary of contacts that are keyed by the full name of the contact.
    • Contact will contain the data for one contact.

JSON

  • This project will open a file containing a list of contacts in JSON.
  • Download this file by Control-clicking, or Right-clicking on the link and select “Save Linked File As…”. Save it on your Desktop.
  • So, what’s JSON?

What’s JSON?

  • JSON is a data transfer format that uses JavaScript to describe objects. It is used heavily by iOS applications that communicate with backend websites.
  • Here’s a simple example that holds the data for one Contact object.
  • The curly braces, {}, indicate that the data is a dictionary.
  • Each line is a key/value pair with the key first.
  • Here’s a JSON example that contains and array of two Contacts.
  • The square brackets, [], define an array. In this case the array contains dictionaries.
  • There’s a comma between each dictionary.

NSJSONSerialization

  • There’s a class that is part of the Foundation class that come with Xcode named NSJSONSerialization.
  • This class can convert the above JSON data into Objective-C NSDictionary objects, NSArray objects, NSString objects, and NSNumber objects and back. It’s pretty cool.

Loading JSON Data

  • First we need to open the file and read it in to our program.
    • The NSData class can hold any kind of digital data, including text.
  • Next, we use NSJSONSerialization to convert the data to Objective-C objects.
  • That’s it? Yup, that’s all it takes. We now have an NSArray of NSDictionary objects.
    • The JSON dictionaries were converted to NSDictionary objects.
    • The JSON array was converted to an NSArray.

Creating Contact Objects

  • Now we can use a for in loop to iterate through the array of dictionaries.

The Contact Class

  • Your Contact class will need the following properties and methods.
    • A property for the contact first name.
    • A property for the contact last name.
    • A property for the contact email address.
    • A property for the contact cell phone number.
    • A method that will return the full name of the contact by combining the first name and the last name.
    • A method override of the description method. The method will return a string that looks like this:

The MyContacts class

  • Your MyContacts class will need the following properties and methods.
    • A property that is an NSMutableDictionary that will hold all the contacts.
    • A method that will load the JSON file and create the dictionary of contacts. This is the message that will be sent to a MyContacts object in the main function.
      • See the above code that loads the JSON file and converts it to Objective-C objects. This is the method that will contain that code.
    • A method that will be sent an NSDictionary that contains data about one contact. The method will return a Contact object that is filled with the data from the NSDictionary.
    • A method that adds a new Contact object to the NSMutableDictionary of contacts. This method needs to check if the variable is nil and create the dictionary if it is.
      • Use the full name property of the Contact class as the key and the Contact object as the value.
    • A method that is sent the full name of a contact and returns the Contact object that is keyed by that full name.

In the main function

  • In the main function of the project you will need to do the following.
    • Create an instance of the MyContacts class.
    • Send the message to this instance that will load the JSON data.
    • Display the entire dictionary of contacts with an NSLog statement of the contacts property.
    • Use the find method in the MyContacts class to find a few Contact objects. Display the found Contact with an NSLog statement.

Output

  • The full output of the program will look like this.