- Navigate Xcode well enough to print to the debugger console,
- Create a static array,
- Use a
for
loop to iterate over an array, - Printout with
NSLog()
by using string interpolation.
You've absorbed a lot of information about programming from that group of readings. At this point, you've read about:
- the general layout of Xcode,
- declaring and defining variables,
- calling methods,
- creating and manipulating string objects,
- wrapping primitives in
NSNumber
objects, - evaluating
if
conditionals, - creating
for
loops, and - holding variables in ordered collections (arrays).
Now it's time put your new knowledge into practice and turn it into skill.
In this lab you'll practice how to combine iterating through an array with string interpolation.
Here's the premise: you're helping out at the Apple Worldwide Developers Conference conference and need to print badges for the speakers. Each speaker's badge needs to say, "Hello, my name is <#name#>."
-
Open the
*.xcodeproj
and navigate to theAppDelegate.m
implementation file. Locate the correct place to write your code in theapplication:didFinishLaunchingWithOptions:
method. -
Create an
NSArray
object namedconferenceSpeakers
to store the list of speaker names as strings:
* Anita Borg
* Alan Kay
* Ada Lovelace
* Aaron Swartz
* Alan Turing
* Michael Faraday
* Grace Hopper
* Charles Babbage
That's quite a lineup!
-
Iterate over the
conferenceSpeakers
array using afor
loop. (Remember, arrays begin at index0
!) UseNSLog()
inside the loop to print"Hello, my name is <#name#>."
for each speaker in the array. Remember to use string interpolation for theNSLog()
. -
Run (
⌘
+R
) the application on your iOS Simulator. In your debugger console you should see the following lines print out.
Hello, my name is Anita Borg.
Hello, my name is Alan Kay.
Hello, my name is Ada Lovelace.
Hello, my name is Aaron Swartz.
Hello, my name is Alan Turing.
Hello, my name is Michael Faraday.
Hello, my name is Grace Hopper.
Hello, my name is Charles Babbage.
It's too bad that Inigo Montoya was already booked.
Instead of using a single array for names, split the names into two separate arrays for first name and last name. Alter your NSLog()
to take format arguments from both arrays: Hello, my name is <#firstName#> <#lastName#>.
. You should get an equivalent result to the one shown in step 4.