Objective-C Protocols

来源:https://www.cnblogs.com/xujinzhong/archive/2018/02/22/8459232.html
-Advertisement-
Play Games

Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. Protocols are implemented in the ...


Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation. Protocols are implemented in the classes conforming to the protocol.

A simple example would be a network URL handling class, it will have a protocol with methods like processCompleted delegate method that intimates the calling class once the network URL fetching operation is over.

A syntax of protocol is shown below.

@protocol ProtocolName
@required
// list of required methods
@optional
// list of optional methods
@end

The methods under keyword @required must be implemented in the classes that conforms to the protocol and the methods under @optional keyword are optional to implement.

Here is the syntax for class conforming to protocol

@interface MyClass : NSObject <MyProtocol>
...
@end

This means that any instance of MyClass will respond not only to the methods declared specifically in the interface, but that MyClass also provides implementations for the required methods in MyProtocol. There's no need to redeclare the protocol methods in the class interface - the adoption of the protocol is sufficient.

If you need a class to adopt multiple protocols, you can specify them as a comma-separated list. We have a delegate object that holds the reference of the calling object that implements the protocol.

An example is shown below.

#import <Foundation/Foundation.h>

@protocol PrintProtocolDelegate

- (void)processCompleted;

@end

@interface PrintClass :NSObject
{
    id delegate;
}

- (void) printDetails;
- (void) setDelegate:(id)newDelegate;
@end

@implementation PrintClass

- (void)printDetails{
    NSLog(@"Printing Details");
    [delegate processCompleted];
}

- (void) setDelegate:(id)newDelegate{
    delegate = newDelegate;
}

@end

@interface SampleClass:NSObject<PrintProtocolDelegate>

- (void)startAction;

@end

@implementation SampleClass

- (void)startAction{
    PrintClass *printClass = [[PrintClass alloc]init];
    [printClass setDelegate:self];
    [printClass printDetails];
}

-(void)processCompleted{
    NSLog(@"Printing Process Completed");
}

@end

int main(int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    SampleClass *sampleClass = [[SampleClass alloc]init];
    [sampleClass startAction];
    [pool drain];
    return 0;
}

Now when we compile and run the program, we will get the following result.

2013-09-22 21:15:50.362 Protocols[275:303] Printing Details
2013-09-22 21:15:50.364 Protocols[275:303] Printing Process Completed

In the above example we have seen how the delgate methods are called and executed. Its starts with startAction, once the process is completed, the delegate method processCompleted is called to intimate the operation is completed.

In any iOS or Mac app, we will never have a program implemented without a delegate. So its important we understand the usage of delegates. Delegates objects should use unsafe_unretained property type to avoid memory leaks.


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 第一範式(1NF):元組的分量不可再分; 第一範式(1NF):元組的分量不可再分; 第二範式(2NF):所有分量唯一決定主鍵碼,不允許部分依賴; 第三範式(3NF):不允許傳遞依賴。 1. 第一範式(1NF) 強調的是列的原子性,即列不能夠再分成其他幾列。 考慮這樣一個表:【聯繫人】(姓名,性別,電 ...
  • 1、儘量不要寫太過複雜的SQL查詢,不要想著非要一次性將結果全部以前端要求的形式返回出來,可以多次分開查詢,這樣邏輯清晰,問題解決速度快,方便維護,並且SQL的效率也高。 2、在使用聯表查詢的時候,關聯表的表長一定不能過長,不然十分影響查詢效率。工作中的實際情況是,當關聯表表長接近1000時,速度就 ...
  • 在日常使用中難免會與XML打交道,其中一個常用的格式就是日期了。 交互的時候通常有下麵2種方式 其中T1 的時間格式是我們平常比較容易看到的。但是也有小部分的時候會遇到T的時間格式,後面帶上時區的時候,就會在取Datetime的時候直接轉換成0時區的時間,所以 這時候如果需要將T和T1 都轉成一致的 ...
  • 本文介紹使用ELK(elasticsearch、logstash、kibana) + kafka來搭建一個日誌系統。主要演示使用spring aop進行日誌收集,然後通過kafka將日誌發送給logstash,logstash再將日誌寫入elasticsearch,這樣elasticsearch就有... ...
  • 基於elasticsearch2.3.3寫的入門demo,含有創建index,刪除index,錄入數據,查詢、以及分頁,並且使用流行的ik分詞器。測試方式:使用 JNUIT TEST 來運行。 ...
  • 一,代碼優化: 代碼優化是任何優化技術的第一步,因為歸根結底網頁上的一切都是構建在代碼之上的。優秀的代碼可以節省寬頻,減少渲染延遲,以及提高頁面的可讀性和長遠的可維護性。下麵列出了一些在Web應用中編寫任何代碼時都應該記住的最佳實踐。 1,使用遵循Web標準的代碼。 2,精簡代碼。 3,減少HTTP ...
  • @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getW ...
  • 在Android Studio項目裡面有個local.properties文件,這個文件可以放一些系統配置。比如:sdk路徑、ndk路徑。 當然我們也可以在local.properties放一些自定義的配置,比如簽名文件: build.gradle 如何讀取local.properties欄位信息 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...