iPhone Programming - Spring 2009

Nathan Hull

Assignment 1- Due: Friday, Feb. 13th


In this assignment you will explore Objective-C, the Foundation Framework and Xcode.

In brief, you will create a program that keeps your user names and passwords for the various logins that modern life seems to require. (Note that since the program neither saves the information to a disk file nor encrypts it, that it is a somewhat incomplete implementation!)

First, in Xcode, you will be creating a New Project. Choose "File:New Project" and then from the left panel choose to create a new "Mac OS" (not iPhone) "Command Line Utility". After these choices have been made, in the right box, select "Foundation Tool". This should provide a good template for your project.

In this homework, you will create a new NSMutableDictionary Object which we will call "login" . The Key for the dictionary is the name of a website, and as its Value includes two Strings (your username and your password), and also an Integer count of the number of times you have logged in. Thus, a possible data set might look something like this:

Key
Value
Username
Password
Count
Gmail
Frankenstein
greenaboutyou
12
Yahoo
Dracula
biteoutatheapple
14
NYU
Wazowski
Sulley1549
89
Amazon
Kate Monster
Monstersorri
14
AOL
Chocula
bad!!teeth
0

 

There are many, many methods for Dictionaries which are given to you in the Foundation Framework. Let's look at one of them. The following is the official Apple definition of setObject:
- (void) setObject:(id)anObject forKey:(id)aKey

In our book, "Programming in Objective-C 2.0" by Stephen Kochan, in Chapter 15 the author gives the following example of this method:

[glossary setObject: @”A class defined so other classes can inherit from it”
          forKey:    @”abstract class”];

In this case, glossary is a Mutable Dictionary which has already been defined and both the Key (forKey) and the Value (setObject) are NSStrings. However, note that neither the Key nor the Value have to be strings, although that is what is most often demonstrated. They can be any kind of object. So, in our homework, although the Key is a string, the Value should be a new kind of object (call it "SiteValue") that consists of the two strings and the Integer Count.

Therefore, part of the problem will be to create a new class, SiteValue, which will be defined in separate Interface and Implementation Files (perhaps SiteValue.h and SiteValue.m). To create these new classes in Xcode, choose "File:NewFile" and then choose "Mac OS: Cocoa" on the left panel and "Objective-C Class" on the right. In the option box that appears, in addition to the name of your new Class, choose to create the .h file at the same time.

In your description of the SiteValue class, in addition to the definition of the three Instance Variables, you will also need to provide Getters and Setters. (Note that the Setter should explicitly set the Count to 0. This also means that you can't use 'Properties' when building the SiteValue class.) There are at least two other methods you should describe:

  1. You should create a method which prints out the SiteValue class in some kind of pretty format using NSLog().
  2. You should write a method that will increment the Count variable of a SiteValue object.

In your main function, you should do (at least) the following operations:

  1. Create the login dictionary.
  2. Add some entries (That is, from hard-coded data!)
  3. Print out the dictionary by enumerating it. (Note that you will have to print the key for each record directly, but that the value portion can be printed by the method you defined in the SiteValue class).
  4. Delete a few records.
  5. Print out the dictionary again.
  6. Go through your entire Dictionary incrementing each record's Count using the method you wrote for SiteValue.
  7. Print out the dictionary yet again.

In addition to the material in your textbook, don't forget that Apple provides you with a rich set of reference material. The iPhone Developers website has lots of information on Objective-C, but much of that material is also available from within Xcode. For example, you can simply choose "Help: Documentation" and type "NSMutableDictionary" in the search box, and a wealth of information is available to you - including a pdf file describing the whole class. (Don't forget to look at the super class "NSDictionary").

Finally as a hint, Adrian notes that it might be wise to define a Class Method as such:

+(SiteValue*) siteValueWithUsername: (NSString*) username
              andPassword: (NSString*) password
              andCount: (int) count;

which creates a new SiteValue and calls the Instance method

-(SiteValue*) initWithUsername: (NSString*) username
              andPassword: (NSString*) password
              andCount: (int) count;


This is pretty convenient when adding things to the login dictionary and seems to be a common idiom.


Zip your entire project, and send it to your TA, Adrian Secord at: s9048004 [at] cs [dot] nyu [edu]

This assignment is a work-in-progress, so check back for any additions, corrections, clarifications, helpful hints, etc., etc.