gpt4 book ai didi

objective-c - 使用委托(delegate)协议(protocol)初始化

转载 作者:太空狗 更新时间:2023-10-30 03:26:35 24 4
gpt4 key购买 nike

每当我创建一个具有符合协议(protocol)的委托(delegate)的 init 时,我都会将 init 写成这样:

- (id)initWithDelegate:(id<ProtocolToConform>)delegate;

这样如果创建的对象不符合协议(protocol)我就会有一个警告。

但是我注意到 UIAlertView init 方法看起来像这样:

- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...

委托(delegate)参数没有指定符合UIAlertViewDelegate协议(protocol)? Apple 这样做的任何线索?

最佳答案

好问题!我做同样的事情,希望在编译时捕获更多错误。

Apple 似乎符合他们自己的标准;如 Concepts in Objective-C 中所述:

To implement a delegate for your custom class, complete the following steps:

Declare the delegate accessor methods in your class header file.

- (id)delegate;
- (void)setDelegate:(id)newDelegate;

Implement the accessor methods. In a memory-managed program, to avoid retain cycles, the setter method should not retain or copy your delegate.

- (id)delegate {
return delegate;
}

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

关于objective-c - 使用委托(delegate)协议(protocol)初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14626634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com