object - c類接口中的尖括號“<…>”用於什么?

[英]What are the angle brackets “<…>” in an Obj-C class interface for?


Can anyone tell me what the angle brackets "<…>" in an Obj-C class interface do? Like this… http://snipt.net/robhawkes/cocoa-class-interface

誰能告訴我object - c類接口中的尖括號<…>是做什么用的嗎?像這樣…http://snipt.net/robhawkes/cocoa-class-interface

@interface MapMeViewController : UIViewController <CLLocationManagerDelegate, 
            MKReverseGeocoderDelegate, MKMapViewDelegate, UIAlertViewDelegate> { ... }

From my view they look like some sort of type declaration (considering my previous experience in PHP and JavaScript), like we're making sure MapMeViewController is a CLLocationManagerDelegate, MKReverseGeocoderDelegate, MKMapViewDelegate, or UIAlertViewDelegate

在我的視圖中,它們看起來像是某種類型的聲明(考慮到我以前在PHP和JavaScript方面的經驗),比如我們要確保MapMeViewController是一個CLLocationManagerDelegate、MKReverseGeocoderDelegate、MKMapViewDelegate或UIAlertViewDelegate

Documentation about the @interface syntax don't seem to mention this.

關於@interface語法的文檔似乎沒有提到這一點。

4 个解决方案

#1


35  

The angle brackets in a class interface definition indicates the protocols that your class is conforming to.

類接口定義中的尖括號表示類遵循的協議。

A protocol is almost like an interface in Java or C#, with the addition that methods in an Objective-C protocol can be optional.

協議幾乎就像Java或c#中的接口,而Objective-C協議中的方法可以是可選的。

Additionaly in Objective-C you can declare a variable, argument or instance variable to conform to several protocols as well. Example

此外,在Objective-C中,您還可以聲明一個變量、參數或實例變量以符合多個協議。例子

NSObject<NSCoding, UITableViewDelegate> *myVariable;

In this case the class must be NSObject or a subclass (only NSProxy and its subclasses would fail), and it must also conform to both NSCoding and UITableViewDelegate protocols.

在這種情況下,類必須是NSObject或子類(只有NSProxy及其子類會失敗),而且它還必須同時符合NSCoding和UITableViewDelegate協議。

In Java or C# this would only be possible by actually declaring said class.

在Java或c#中,這只能通過實際聲明類來實現。

#2


9  

The angle brackets indicate a protocol. They're analogous to interfaces in other languages.

尖括號表示協議。它們類似於其他語言中的接口。

#3


6  

You can also use them in code like a cast to tell the complier to expect an object that conforms to a particular protocol.

您還可以在代碼中使用它們,像cast這樣告訴編譯器期望一個符合特定協議的對象。

id <NSFetchedResultsSectionInfo> sectionInfo = [[self.noteFetcher sections] objectAtIndex:section];

#4


4  

Apple documentation reports the use of brackets; see The Objective-C Programming Language on the chapter 4, on "Adopting a Protocol".

蘋果文檔報告使用了括號;請參閱第4章關於“采用協議”的Objective-C編程語言。

Adopting a protocol is similar in some ways to declaring a superclass. Both assign methods to the class. The superclass declaration assigns it inherited methods; the protocol assigns it methods declared in the protocol list. A class is said to adopt a formal protocol if in its declaration it lists the protocol within angle brackets after the superclass name:

采用協議在某些方面與聲明超類相似。兩者都為類分配方法。超類聲明分配它繼承的方法;協議分配它在協議列表中聲明的方法。如果一個類在其聲明中在父類名稱后的尖括號內列出了該協議,則稱為采用正式協議:

@interface ClassName : ItsSuperclass < protocol list >

Categories adopt protocols in much the same way:

類別采用協議的方式大致相同:

@interface ClassName ( CategoryName ) < protocol list >

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/12/16/a843cf632f02787a6172968655fafe56.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com