Now that we have our basic class working, we can play with it a bit. First I want to introduced an easier way of allocating and initializing your instances. In part 4, I used 2 lines of code to setup an instance of a class. One line allocated it, and the other initialized it. Both these steps can be done in one line, as shown below:
PrintCount * phraseb = [[PrintCount alloc] init]
This does the exact same thing as the 2 lines of code used before, just less typing. You can do it the old way if you want, but this is considered the convention when coding ObjC. Remember that, like in math, the innermost brackets are calculated first, then they work outward. Nested expressions can be used in many places in your code, We will discuss them more when we build upon our class in a future post.
So what if you wanted to count the number of times you printed another line of text? Simple, just invoke another instance of our class, then use the same methods we have before. Here is an example using 3 instance of our PrintCount class:
PrintCount * phraseA = [[PrintCount alloc]init];
PrintCount * phraseB = [[PrintCount alloc]init];
PrintCount * phraseC = [[PrintCount alloc]init];
[phraseA setText: @"This is Phrase A, Hello!"];
[phraseB setText: @"This is Phrase B, How are you?"];
[phraseC setText: @"This is Phrase C, I am fine!"];
[phraseA printText];
[phraseB printText];
[phraseC printText];
[phraseA printNum];
[phraseB printNum];
[phraseC printNum];
This code should output:
2011-03-16 11:39:53.797 HelloOOP[23059:a0f] This is Phrase A, Hello!
2011-03-16 11:39:53.799 HelloOOP[23059:a0f] This is Phrase B, How are you?
2011-03-16 11:39:53.800 HelloOOP[23059:a0f] This is Phrase C, I am fine!
2011-03-16 11:39:53.801 HelloOOP[23059:a0f] The string 'This is Phrase A, Hello!' has been printed 1 times
2011-03-16 11:39:53.801 HelloOOP[23059:a0f] The string 'This is Phrase B, How are you?' has been printed 1 times
2011-03-16 11:39:53.802 HelloOOP[23059:a0f] The string 'This is Phrase C, I am fine!' has been printed 1 times
Hopefully this is starting to show the true power of classes. The deeper we get into programming for iOS devices, the more we will uses classes. In fact, the foundation library defines a bunch of classes we are already using, like NSString and NSLog. Almost everything we will use is part of a class somehow!
Next up we are going to be building up our class a bit and using the additions to learn about if Statements, For/while loops, and finally a simple iPhone application. This should get us comfortable enough so we don't feel lost starting the Stanford iOS Development course. If the CS193P section this year is not online, we will use the Winter 2010 one. As a bit of a note, From here on out, I’ll be using Xcode 4 unless for some random reason CS193P uses the older version. Even in that case I may still use it and translate everything into Xcode 4 anyway to get accustomed to it.
So are there any ObjectiveC or iOS topics YOU think we should hit? Let me know in the comments!