相关疑难解决方法(0)

建议使用ARC声明委托属性的方法

我曾经将所有委托属性声明为

@property (assign) id<FooDelegate> delegate;
Run Code Online (Sandbox Code Playgroud)

我的印象是所有赋值属性现在都应该是弱指针,这是正确的吗?如果我试图声明为:

@property (weak) id<FooDelegate> delegate;
Run Code Online (Sandbox Code Playgroud)

我在尝试@synthesize时遇到错误(不支持自动生成的弱属性).

在这种情况下,最佳做法是什么?

cocoa-touch delegates objective-c automatic-ref-counting

31
推荐指数
2
解决办法
2万
查看次数

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

注意:以下是使用启用了自动引用计数(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'.我得到的错误是; …

cocoa-touch protocols objective-c

5
推荐指数
2
解决办法
8886
查看次数