Objective-C Protocols,objective-c

來源:互聯網
上載者:User

Objective-C Protocols,objective-c

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 的對象。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.