Lab 2 - Objective-C Basics
Objectives
- Write a simple Objective-C program
- Use the Foundation Classes
You will be writing a simple Objective-C program which creates and uses
objects from the Foundation Classes.
- In Xcode, create a new Project and select Command Line Utility and
create a Foundation Tool project template.

- Open the project's .m file.You should see a skeletal main() program has
been written already. Open the Console under Run->Console and try it
out.
- Check out the documentation for NSString either
here or in Xcode using Help->Documentation and then searching for the
class. This class has a set of methods that allow you to manipulate file
system paths (under the section in the documentation "Working with Paths").
We will start by creating a NSString constant for a tilde. String
constants are always preceded by the @ symbol.
NSString *path = @"~";
Use the appropriate method that will expand the tilde in the path to the
full path to your home directory and store the result back into the path
variable. (This is a common convention to reuse the variable when using
convenience methods). Use the NSLog statement to display something
like:
My home folder is at 'Users/jzimmerm'
- The NSString class has a method that will return an array of path
components, where each element in the array is a single component in the
original path. Use this method to get an NSArray of the path component
for the path you just logged.
Now use fast enumeration to display each of these components so that it will
appear like:
/
Users
jzimmerm
Fast enumeration looks like:
for (NSString *s in myArray)
NSLog(s);
- Look up the NSMutableDictionary and NSURL classes.
Create a mutable dictionary that contains the following key/value pairs:
Key (NSString)
Value (NSURL)
Goucher College
http://www.goucher.edu
Apple
http://www.apple.com
CS325
http://phoenix.goucher.edu/~jillz/cs325
Goucher IT
http://www.goucher.edu/x5912.xml
You will want to use +URLWithString: to create URL instances.
Enumerate through the keys of the dictionary and check each key to see if it
starts with @"Goucher". If it does, retrieve the NSURL value for that
key and log both the key and the NSURL. If it doesn't then no output
should be generated for that key.
Your output should look like:
Key: 'Goucher College' URL: 'http://www.goucher.edu'
When using a dictionary and fast enumeration, you are enumerating the keys
of the dictionary. You can then use the key to retrieve the value.
- Compress your project and send it to me through the Dropbox in
BlackBoard.