I made myself familiar with some Obj-c syntax here, as well as basic app structure. Things a complete novice (such as myself) should keep in mind:
NSString
To create an NSString:
NSString *myString = @”This is my new NSString”;
To output different types within my string I need to use stringWithFormat, and the right format specifier to define that type. For example, I can’t assign an integer type as part of an NSString. The full list of specifiers is• here. Here are the specifiers I’ve most commonly used:
- %@: Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise. Also works with CFTypeRef objects, returning the result of the CFCopyDescription function.
- %d, %D, %i: Signed 32-bit integer (int)
- %u, %U: Unsigned 32-bit integer (unsigned int)
- %f 64-bit floating-point number (double)
The format for using these is:
NSString *myString = [NSString stringWithFormat:@"Here is an int:%d, and an Object:%@", 42, myObject];
I can append a string using the aptly named stringByAppendingString :
NSString *beginString = @”The quick brown fox”;
NSString *nextString = @” jumped over the lazy dog”;
NSLog([beginString stringByAppendingString: nextString]);
which will output:
“The quick brown fox jumped over the lazy dog”
to the console.
NSDictionary
There are a lot of ways to get values into an NSDictionary. The API reference is a great place to look for details about that. For this assignment I used dictionaryWithObjects:forKeys
I created key objects and stuffed them into an array:
NSString *firstString = @”Stanford University”;
NSString *secondString = @”Apple”;
NSArray *keyArray = [NSArray arrayWithObjects:firstString, secondString, nil];
(There was some initial frustration here because I didn’t realize that when populating/creating an array this way I needed to terminate the array with a nil value so that arrayWithObjects knows that it has reached the end of the array. Not necessary with other types of instantiation)
then value objects:
NSURL *firstURL = [NSURL URLWithString:@"http://www.stanford.edu"];
NSURL *secondURL = [NSURL URLWithString:@"http://www.stanford.edu"];
NSArray *valueArray = [NSArray arrayWithObjects:firstURL, secondURL, nil]
…and into the Dictionary they go:
NSDictionary *myDict = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
Enumerator:
(assuming obj1 – obj5 have been created…)
NSMutableArray *lotsOfObjects = [NSMutableArray arrayWithObjects:obj1, obj2, obj3, obj4, obj5, nil];
carve out a space for the enum:
NSEnumerator *en = [lotsOfObjects objectEnumerator];
and give it something to store the iterated object’s value in (id is a generic object type (for lack of the desire to go looking for a better description right now))
id obj = nil;
then use it:
while (obj = [en nextObject]) {
//do something with obj
}