Objective-C Protocols?
1.1 Formal Protocols?
A formal protocol (like an informal protocol) is a named list of methods and properties.
formal protocol (像informal protocol)是一系列方法和属性的名字列表。
However, a formal protocol requires that you explicitly adopt it.
formal protocol 需要你明确地采纳它。
You adopt a protocol by listing the protocol's name in your class's @interface declaration.
你通过在你的类@interface中列出protocol 的名字来采用这个协议。
When you do this, your class is said to conform to the protocol
当你采用时,你也就是顺从了这个协议。
Adopting a protocol means that you promise to implement all the methods of that protocol. If you don't, the compiler yells at you by generating a warning.
当你采用这个protocol 意味着你许诺实现所有的protocol所有的方法。如果你不采用的话,编译器就会对此产生一个警告。
?
Formal protocols are just like Java interfaces. In fact, Objective-C protocols were the inspiration for Java's interfaces.
formal protocols 很像java的接口
1.2 Declaring Protocols声明 protocols?
?
If you adopt NSCopying, your object knows how to make copies of itself:
@protocol NSCopying
- (id) copyWithZone: (NSZone *) zone;
@end
The syntax looks kind of the same as the syntax for declaring a class or a category.
从句法上看 很像声明一个类或category 的句法。
You can also can have parent protocols, similar to parent classes. To specify the parent protocol, declare it in angle brackets after the name of the protocol.
你可以像有parent classes 那样有parent protocols.为了声明一个parent protocol ,必须把父类声明在一个尖括号里面,在一个protocol 后面。
@protocol MySuperDuberProtocol <MyParentProtocol>
@end
The preceding lines mean that MySuperDuperProtocol extends MyParentProtocol, so you have to satisfy the method implementation of all the required methods in both protocols.
你必须实现在这两个协议里要求的所有方法。
?
@protocol NSCoding
- (void) encodeWithCoder: (NSCoder *) encoder;
- (id) initWithCoder: (NSCoder *) decoder;
@end
1.3 Adopting a Protocol 采用一个protocol?
To adopt a protocol, you list the protocol in the class declaration, surrounded by angle brackets.
当你要采用一个protocol,你把协议放在类声明里,被尖括号环绕。
@interface Car : NSObject <NSCopying>
{
// instance variables
}
// methods
@end // Car
And if Car adopts both NSCopying and NSCoding, the declaration goes like this:
@interface Car : NSObject <NSCopying, NSCoding>
{
// instance variables
}
// methods
@end // Car
When you adopt a protocol, you're sending a message to programmers reading the class declaration, saying that objects of this class can do two very important things: they can encode/ decode themselves and copy themselves.
当你采用了protocol ,你再向一个编程者发送一个信息:这个对象能实现两个非常重要的事情。
?
1.4Copies?
The copy method, of course, makes a copy of an object. The copy message tells an object to create a brand new object and to make the new object the same as the receiver.
当一个copy方法,为一个对象做了一个备份。这个copy信息将告诉一个对象创建一系列新的对象,并且让新的对象和消息接受者的对象一样。
Making Copies?
Most objects refer to—that is, point at—other objects.
大部分objects 引用,也就是指向一个对象。
When you create a shallow copy, you don't duplicate the referred objects; your new copy simply points at the referred objects that already exist. NSArray's copy method makes shallow copies. When you make a copy of an NSArray, your copy only duplicates the pointers to the referred objects, not the objects themselves. If you copy an NSArray that holds five NSStrings, you still end up with five strings running around your program, not ten. In that case, each object ends up with a pointer to each string.
当你要创建一个浅度复制,你没有复制被指向的对象。你新的复制仅仅指向被指向的已经存在的对象。
A deep copy, on the other hand, makes duplicates of all the referred objects. If NSArray's copy was a deep copy, you'd have ten strings floating around after the copy was made.
?
@interface Engine : NSObject <NSCopying>
@end // Engine
Because we've adopted the NSCopying protocol, we have to implement the copyWithZone: method.
因为我们采用了NSCopying协议,我们必须实现copyWithZone方法。
A zone is an NSZone, which is a region of memory from which you can allocate memory. When you send a copy message to an object, it gets turned into copyWithZone: before reaching your code.
一个zone是一个NSZone,他是你能够分配内存的区域。当你发送一个copy 消息给一个对象的时候,它在接触你的代码之前就转向一个copyWithZone方法了。
Here's Engine's copyWithZone:
implementation:
- (id) copyWithZone: (NSZone *) zone
{
? ? Engine *engineCopy;
? ? engineCopy = [[[self class]
? ? allocWithZone: zone] init];
? ? return (engineCopy);
} // copyWithZone
By using [self class], the allocWithZone: will be sent to the class of the object that is receiving the copy message.
如果使用[self class ], allocWithZone将被传递这个对象的类,接受copy信息。
@interface AllWeatherRadial : Tire
// ... properties
// ... methods
@end // AllWeatherRadial
When AllWeatherRadial inherits from Tire, it pulls along all of Tire's baggage, including the conformance to the NSCopying protocol.
We'll need to implement copyWithZone: though, because we have to make sure AllWeatherRadial's rain- and snow-handling instance variables are copied:
- (id) copyWithZone: (NSZone *) zone
{
?AllWeatherRadial *tireCopy;
?tireCopy = [super copyWithZone: zone];
?tireCopy.rainHandling = rainHandling;
?tireCopy.snowHandling = snowHandling;
?return (tireCopy);
} // copyWithZone
1.5 Protocols and Data Types
You can specify protocol names in the data types you use for instance variables and method arguments.
你可以在你的数据类型中作为实力变量和方法参数指定protocol 名字
?
Recall that the id type represents a pointer to any kind of object; it's the generic object type.
一个id类型指明了一个可以指向任何类的指针。它使一般的对象类型。
You can assign any object to an id variable, and you can assign an id variable to any kind of object pointer.
你可以分配任意对象给一个id 变量,你可以分配任意id变量给任意一个对象指针。
If you follow id with a protocol name, complete with angle brackets, you're telling the compiler (and any humans reading the code) that you are expecting any kind of object, as long as it conforms to that protocol.
如果通过一个协议名字前面加一个id,你在告诉编译器你可以有任何对象。
- (void) setObjectValue: (id<NSCopying>) object;
1.6 新特性
. Objective-C 2.0 added two new modifiers for protocols: @optional and @required.
Objective-C2.0 给protocol添加了两个新的修改:@optinoal and @required?
@protocol BaseballPlayer
- (void)drawHugeSalary;
@optional
- (void)slideHome;
- (void)catchBall;
- (void)throwBall;
@required
- (void)swingBat;
@end // BaseballPlayer
1.7 The Delegation Will Come to Order
Delegation is a design pattern that allows an object to designate another object to handle a particular task.
delegation 是一种设计模式:允许一个对象指定另外一个对象执行特殊的任务。
?
- (id <NSNetServiceBrowserDelegate>)delegate;
- (void)setDelegate:(id <NSNetServiceBrowserDelegate>)delegate;
The first method returns the current delegate if it is set, or nil otherwise.
第一个方法返回现有的委托如果已经设置的话,或者返回nil。
he second one sets the delegate. The type of the argument delegate tells us that we can set any object as a delegate as long as it conforms to the expected protocol.
第二种方法设置delegate。委托的参数类型告诉我们能够设置任意的顺从预定protocol 的对象。