Chapter 10 Object Initialization
Allocating Objects
Allocation is the process by which a new object is born. alloc also conveniently initializes all the memory to 0.
All your BOOLs start out as NO; all your ints are 0; all your floats become 0.0; all your pointers are nil
Initializing Objects
An init method can return nil if there’s a problem initializing an object.
Many classes have convenience initializers. These are init methods that do some extra work, saving you the trouble of doing it yourself. (e.g. initWithFormat)
The designated initializer & subclass problem P 220
One init method in a class is the designated initializer. All the initializer methods of the class use the designated initializer to do the initialization work. Subclasses use their superclass’s designated initializer for their superclass initialization.
The init method that takes the most arguments usually ends up being the designated initializer. If you’re using someone else’s code, be sure to check the documentation to see which method is the designated initializer.
Initializer Rules
You’re not required to create an initializer method for your class.
If you do write an initializer, be sure you call the superclass’s designed initializer in your own designated initializer.
If you have more than one initializer, pick one to be the designated initializer.
Chapter 11 Properties
All @property is doing is automatically declaring the setter and getter methods for the attribute. The attribute doesn’t actually have to match the name of the instance variable, but it will in most cases.
@synthesize is a new compiler feature that says “create the accessors for this attribute.” For the line of code @synthesize rainHandling;, the compiler emits the compiled code for -setRainHandling: and -rainHandling.
Instance variable name vs. property name P 236
getter/setter and dot syntax will always use the property name! but we can relate property name to corresponding instance variable name through @synthesize
Chapter 12 Categories
A category is a way to add new methods to existing classes.
Categories have two limitations.
- The first is that you can’t add new instance variables to a class. There’s nowhere to put them.
- The second limitation concerns name collisions, in which one of your category methods has the same name as an existing method. When names collide, the category wins. Your category method will completely replace the original method, with no way of getting
the original back.
In Cocoa, categories are used mainly for three purposes:
- splitting a class’s implementation across multiple files or multiple frameworks,
- creating forward references for private methods
- adding informal protocols to an object
Making Forward References with Categories
If compiler sees you calling a method on an object, and it hasn’t seen a declaration or definition for that method yet, it complains like this: warning: 'CategoryThing' may not respond to '-setThing4:'.
Our technique is often to place a category at the top of the implementation file.
@interface Car (PrivateMethods)
- (void) moveTireFromPosition: (int) pos1
toPosition: (int) pos2;
@end // Private MethodsWhen you implement this method, it doesn’t have to exist in an @implementation Car (PrivateMethods) block. You can leave it in the @implementation Car section. This lets you separate your methods into categories as an organizational and documentation
convenience, while still allowing you to keep all your methods in one big pile in the implementation file.Informal Protocols and Delegation Categories
Cocoa classes often use a technique that involves a delegate, which is an object asked by another object to do some of its work.
Putting a category on NSObject is called creating an informal protocol.
Responds to Selectors
What is a selector? It’s just the name of a method, but it’s encoded in a special way that’s used by the Objective-C runtime for quick lookups.
You indicate a selector by using the @selector() compiler directive, with the name of the method nestled in the parentheses.
@selector(setEngine:)
@selector(setTire:atIndex:)NSObject provides a method called respondsToSelector: that queries an object to see if it will respond to a given message.Car *car = [[Car alloc] init];
if ([car respondsToSelector: @selector(setEngine:)]) {
NSLog (@"yowza!");
}Selectors can be passed around and used as arguments to methods and even stored as instance variables.
本文详细介绍了Objective-C中对象的初始化过程及内存分配,包括如何使用init方法进行初始化和初始化子类时如何调用超类的初始化方法。同时阐述了属性的自动管理方式,通过@property自动创建setter和getter方法,并解释了实例变量名与属性名的关系。最后讨论了类别(Category)的作用,如何使用类别添加新方法到现有类,并介绍如何创建代理(Delegate)和使用类别实现前向引用。

被折叠的 条评论
为什么被折叠?



