[英]Objective-C warning: Class 'ViewController' does not conform to protocol 'CBPeripheralManagerDelegate'
[英]Is it possible for a class to conform to more than one protocol in objective-c?
一個類是否可能符合Objective-C中的多個協議? 如果是這樣,則聲明一個符合多個協議的類的語法是什么?
@interface MyClass : NSObject <Protocol1, Protocol2, Protocol3>
@end
是; 只需在每個協議之間使用逗號即可。
是的,一個類可能符合多種協議。 語法如下:
@interface MyClass : NSObject <Protocol1, Protocol2, Protocol3>
//...Some code here...
@end
Objective-C中的協議本質上是方法的列表,必須執行這些方法才能使對象或類符合該協議。 符合多種協議的類的常見示例是充當UITableViewDataSource和UITableViewDelegate的UITableViewController。
對於UITableViewController示例,它可能如下所示:
@interface MyTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
//...Some code here...
@end
您用逗號分隔每個協議,並將其放在這些括號內。 當您將這些協議添加到接口聲明中時,您實際上是在說“是的,我將實現那些協議定義的方法”。 現在,繼續執行這些方法,否則編譯器會提醒您您沒有遵守諾言。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.