!!!Obj-c on Mac --- Chapter 10 - 12 Initialization Property Category

来源:互联网 发布:mac如何显示包内容 编辑:程序博客网 时间:2024/06/07 01:51

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) pos1toPosition: (int) pos2;@end // Private Methods
When 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.

0 0
原创粉丝点击