无法在iOS上使用自定义@protocol

zio*_*zio 5 cocoa-touch protocols objective-c

注意:以下是使用启用了自动引用计数(ARC)的iOS.我认为ARC可能与它为什么不起作用有很大关系,因为这是根据我通过谷歌发现的例子设置的.

我正在尝试创建一个协议来通知代表用户从UITableView中选择的文件名.

FileListViewController.h

@protocol FileListDelegate <NSObject>
- (void)didSelectFileName:(NSString *)fileName;

@end

@interface FileListViewController : UITableViewController
{
    @private
        NSArray *fileList;
        id <FileListDelegate> delegate;
}
@property (nonatomic, retain) NSArray *fileList;
@property (nonatomic, assign) id <FileListDelegate> delegate;

@end
Run Code Online (Sandbox Code Playgroud)

FileListViewController.m

#import "FileListViewController.h"

@implementation FileListViewController

@synthesize fileList;
@synthesize delegate;
Run Code Online (Sandbox Code Playgroud)

这给出了一个错误

@synthesize delegate;
Run Code Online (Sandbox Code Playgroud)

行"FileListViewController.m:错误:自动引用计数问题:unsafe_unretained属性'委托'的现有ivar'委托'必须是__unsafe_unretained"

如果我改变FileListViewController.h放置__weak和(弱)然后它将运行.

@protocol FileListDelegate <NSObject>
- (void)didSelectFileName:(NSString *)fileName;

@end

@interface FileListViewController : UITableViewController
{
    @private
        NSArray *fileList;
        __weak id <FileListDelegate> delegate;
}
@property (nonatomic, retain) NSArray *fileList;
@property (weak) id <FileListDelegate> delegate;

@end
Run Code Online (Sandbox Code Playgroud)

但是当我尝试设置代理时,应用程序崩溃了.一个名为'ImportViewController'的视图正在从'FileListViewController'创建一个视图并将委托设置为自己(ImportViewController),因此我可以实现我的自定义协议'didSelectFileName'.我得到的错误是;

*终止应用程序由于未捕获的异常"NSInvalidArgumentException",原因:" - [ImportViewController setDelegate:]:无法识别的选择发送到实例0x6c7d430"

我正在运行的代码是;

ImportViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FileListViewController *fileListViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"filelist"];

    [fileListViewController setDelegate:self];
    [self.navigationController pushViewController:fileListViewController animated:YES];

}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  • 为什么推杆(弱)和__弱让它起作用?我不明白为什么这个工作,因为我发现它谷歌搜索,没有解释.
  • 为什么我不能使用'[fileListViewController setDelegate:self];'来设置我的委托.?似乎编译器不知道"委托"存在.

Chr*_*lay 13

在ARC ivars下默认为strong.所以错误

Automatic Reference Counting Issue: Existing ivar 'delegate' for unsafe_unretained property 'delegate' must be __unsafe_unretained"
Run Code Online (Sandbox Code Playgroud)

告诉你,你已经宣布了一个拥有__unsafe_unretained(分配)所有权的财产,其中底层的ivar拥有__strong所有权,这是非法的.为避免错误,您有3个选项:

  1. 省略伊娃.没有必要为合成属性声明ivar.ivar将隐含声明,所有权与您的财产相匹配.
  2. 定义ivar以匹配您的(assign)属性声明: __unsafe_unretained id <FileListDelegate> delegate;
  3. 定义属性以匹配ivar的隐式__strong所有权: @property (weak) id <FileListDelegate> delegate;

就个人而言,我省略了ivar声明,因此您在财产声明中将所有权语义放在一个地方.


小智 4

似乎与:

FileListViewController *fileListViewController =
    [self.storyboard instantiateViewControllerWithIdentifier:@"filelist"];
Run Code Online (Sandbox Code Playgroud)

你没有得到一个FileListViewController对象。看看它说的消息:

-[ImportViewController setDelegate:]: unrecognized selector sent to instance 0x6c7d430

这就是你的应用程序崩溃的原因。还尝试定义一个保留属性,而不仅仅是分配,以防委托在其他地方释放,您的应用程序不会崩溃。