前面提到了OOP的繼承,但不像C++可以有多重繼承,Objective-C是單一繼承的,如果想要做到一個類別同時擁有多種型別的能力,可以透過實作其它型別的interface來達成這個目的。在Java/AS3是用”interface”這個關鍵字,在Objective-C則是用”@protocol”。(有寫過Java/AS3的要特別注意不要把interface跟protocol搞混了,在Objective-C的interface等於Java/AS3的class,而protocol則是相當於interface)
直接來看看要怎麼做吧。如果你要新增一個自定的protocol的話,可以直接在你的專案裡新增一個protocol檔:
當然,你要全部寫在一起也沒人反對,只是為了模組化以及以後的可重複使用考量,建議獨立出來另外寫。新增完成之後(它是一個header檔),就可以開始來寫了,程式碼如下:
在Objective-C裡的protocol是用@protocol
這個語法來定義的。在上面這段程式碼裡,我放了兩個方法,但沒有寫內容。接下來如果我要實作自這個protocol的話,所有定義在@protocol裡的方法都得實作出來。另外,在Objective-C
2.0之後加了@required
跟@optional
的語法,可以讓你設定這個method是不是必需一定要實作的項目。用法如下:
如果沒特別標明的,預設是@required。如果你要實作這個protocol的話,照英文字面來看,@required的部份是規定要實作的,@optional的話就隨你高興了。要注意的是@required跟@optional這兩個語法的影響範圍,是從它以下所有的method都會被影響,直到另一個directive或是@end為止,所以如果你要省略@required的話,記得那些method要寫在@optional前面。接下來來看看要怎麼實作這個protocol:
實作protocol的方法就是用”<>”標記,裡面放protocol的名稱。並不限定只能實作一個protocol,如果要實作多個protocol的話,則是用逗點分開:
因為到目前為止,我們都還沒實作那個protocol裡定義的方法,所以這時候如果直接按下Build的話,就會跳出警告訊息:
接著來把該做的填一填吧。因為在protocol的地方已經有定義好了方法,所以在@interface的地方就不用再特別寫一次,只要在@implementation裡補上該實作的方法就行了。
如果你實作了所有@required的方法的話,則稱為遵守(conform)或採納(adopt)這個protocol(硬翻成中文還是覺得怪怪的,還是英文比較簡潔直接)。若要檢查某物件是否有乖乖遵守某個protocol的規定:
protocol本身也可以像一般類別的繼承,例如:
這時如果你要實作protocol B,則methodA跟methodB都需要實作。
另外,你也可以把protocol拿來當一般的型別定義來用,例如:
表示說這個some_object是個有實作Drawable這個protocol的物件,在編譯階段就可以先做型別檢查。當然也可以一次多個,一樣用逗點分開:
上面提到的這種用@protocol來定義方法的,稱做formal
protocol
,從名字看大概猜得出來一定也有叫做informal
protocol
的東西,不過這個會在category的部份再做說明。
相关推荐
2 Programming in Objective-C 3 6 7 Compiling and Running Programs Using Xcode 8 Using Terminal Explanation of Your First Program Displaying the Values of Variables Summary 25 Exercises 25 3 Classes, ...
基于protocol的iOS模块路由和依赖注入框架.zip,Interface-oriented router for discovering modules, and injecting dependencies with protocol in Objective-C and Swift.
详解 objective-c中interface与protocol的作用 以前对Objective-C中的interface,即头文件的作用一直不太清楚。最近看了一些文章,再加上自己的试验,对头文件的作用稍有了解。 在我看来,头文件的作用是,定义对外...
它在sqlite之上构建,面向Swift和Objective-C开发人员,针对macOS,iOS,tvOS和watchOS。你好,世界: // Create and/or Open the database filelet db = YapDatabase ()// Configure database:// We're going to ...
The Dynamism of Objective-C 57 Extensions to the Objective-C Language 58 Using Objective-C 64 The Root Class 66 NSObject 67 Root Class—and Protocol 67 Overview of Root-Class Methods 68 Interface ...
Chapter 13, Using Mix and Match, explains mix and match and demonstrates how you can include Swift code in your Objective-C projects and Objective-C code in your Swift projects. Chapter 14, ...
Paperback: 395 pages Publisher: Packt ...Master Objective-C interoperability with Mix and Match Access network resources using Swift Implement various standard design patterns in the Swift language
4. Difference between protocol in objective c and interfaces in java 7. What are
C语言程序设计(Visual+C 6.0环境) http://download.csdn.net/source/2232878 Visual C++ 60 MFC + code 学习最强宝典 http://download.csdn.net/source/2236266 ASP.NET Web (第一次亲密接触ASP.NET) ...
What You Will Learn Get to know about the basic data structures and how to use the Swift REPL Use the Swift Standard Library collections bridging to Objective-C collections, and find out about ...
Swift is fundamentally different from Objective-C, as it is a protocol-oriented language. While you can still write normal object-oriented code in Swift, it requires a new way of thinking to take ...
Use the Swift Standard Library collections bridging to Objective-C collections, and find out about protocol-oriented programming Find out about Swift generators and sequences, and see how to use them ...
Find out how Swift communicates with Cocoa’s C and Objective-C APIs Once you master the fundamentals, you'll be ready to tackle the details of iOS app development with author Matt Neuburg's companion...
Find out how Swift communicates with Cocoa’s C and Objective-C APIs Once you master the fundamentals, you’ll be ready to tackle the details of iOS app development with author Matt Neuburg’s ...
Meant for a reader who knows object-oriented programming, has some experience with Objective-C/Swift programming languages and wants to further enhance his skills with functional programming ...
Meant for a reader who knows object-oriented programming, has some experience with Objective-C/Swift programming languages and wants to further enhance his skills with functional programming ...
Swift has a lot of functional programming features that can be easily used, but most Objective-C and Swift programmers are not familiar with these tools. This book aims at simplifying the functional...
Book Description Swift is Apple's innovative development language ... Integrating with Objective-C Chapter 9. Dealing with Other Languages Chapter 10. Data Access Chapter 11. Extensions, Photos, and More
Master Objective-C interoperability with mix and match Access network resources using Swift Implement various standard design patterns in the Swift language In Detail The Swift––Developing iOS ...
It moves on to Rogue Amoeba's Mike Ash explaining how to design a network protocol using UDP, and demonstrating its use in a peer-to-peer application—a topic not normally for the faint of heart, but...