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

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 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值