《preface》
The purpose of this book is to proceed by focusing on the underlying knowledge needed for an actual understanding of iOS programming. That is precisely the opposite of a cookbook. This book has no simple recipes ready for you to drop into your own code and come up with an app. I don’t give you some fish; I teach you what a fish is and what you need to know to obtain one. The number of books out there that skip blithely past the fundamentals, leaving the reader to pick up Objective-C somehow independently, is painfully huge. The result is that numerous learners are encouraged to try their hand at programming when, to judge from the nature of the questions they ask and the confusion they exhibit, they don’t understand what they are doing.
<chart 03>
The tie between a pointer, an instance, and the class of that instance is so close that it is natural to speak of an expression like MyClass*
as meaning “a MyClass instance,” and of a MyClass*
value as “a MyClass.” A Objective-C programmer will say simply that, in the previous example, s
is an NSString, that uppercaseString
returns “an NSString,” and so forth.
Declaration without initialization, before the advent of ARC in iOS 5 (Chapter 12), created a dangerous situation:
NSString* s;
NSString* s = nil; // now s is nil NSString* s2 = [s uppercaseString]; // now s2 is nil
Declaring a Method
The declaration for a method has three parts:
- Either
+
or-
, meaning that the method is a class method or an instance method, respectively. - The data type of the return value, in parentheses.
- The name of the method, split after each colon. Following each colon is the corresponding parameter, expressed as the data type of the parameter, in parentheses, followed by a placeholder name for the parameter.
So, for example, Apple’s documentation tells us that the declaration for the UIColor class method colorWithRed:green:blue:alpha:
is:
+ (UIColor*) colorWithRed: (CGFloat) red green: (CGFloat) green blue: (CGFloat) blue alpha: (CGFloat) alphaMake very sure you can read this declaration! You should be able to look at it and say to yourself instantly, “The name of this method is
colorWithRed:green:blue:alpha:
. It’s a class method that takes four CGFloat parameters and returns a UIColor.”
id
is a pointer, so you don’t say id*
. It is defined to mean “an object pointer,”。Chapter 4. Objective-C Classes
Chapter 5. Objective-C Instances
Ready-Made Instances;
Instantiation from Scratch;
Initialization;
self
does not actually mean “in the same class.” It’s an instance, after all, not a class. What instance? It’s this same instance. The same as what? The same instance to which the message was sent that resulted in the keyword
self
being encountered in the first place.
The Keyword super
The keywordsuper
is class-based, and it means: “Start the search for messages I receive in the superclass of this class” (where “this class” is the class where the keyword
super
appears).
Instance Variables and Accessors
number
instance variable.
Key–Value Coding
Chapter 7. Nib Management
it is based on the file extension .nib, which originated as an acronym for “NeXTStep Interface Builder”.NSMainNibFile
, see
Chapter 6), it is loaded automatically as the app launches; but this is an exceptional case, and has now fallen out of favor — there are no automatically loaded main nib files in the current project templates.
The three most common ways of telling a nib to load are:
- This is UIViewController’s designated initializer. It tells the UIViewController to which it is sent to prepare to load the named nib with itself as owner. (That’s what’s happening in the line of code from AppDelegate.m that I quoted just a moment ago.) The moment of actual loading is automatically postponed until the nib is needed. I’ll talk much more about this in Chapter 19.
-
An NSBundle instance method. Usually, you’ll direct it to
[NSBundle mainBundle]
. Observe that the owner is one of the parameters. I’ll show an example in the next section of this chapter. - A UINib instance method. The nib in question was specified when UINib was instantiated. Again, the owner is one of the parameters. An example appears in Chapter 21.
initWithNibName:bundle:
loadNibNamed:owner:options:
instantiateWithOwner:options: