How do I create delegates in Objective-C?

后端 未结 19 2582
一整个雨季
一整个雨季 2020-11-21 04:48

I know how delegates work, and I know how I can use them.

But how do I create them?

19条回答
  •  [愿得一人]
    2020-11-21 05:01

    When using the formal protocol method for creating delegate support, I've found that you can ensure proper type checking (albeit, runtime, not compile time) by adding something like:

    if (![delegate conformsToProtocol:@protocol(MyDelegate)]) {
        [NSException raise:@"MyDelegate Exception"
                    format:@"Parameter does not conform to MyDelegate protocol at line %d", (int)__LINE__];
    }
    

    in your delegate accessor (setDelegate) code. This helps minimize mistakes.

提交回复
热议问题