【问题标题】:Under IOS5, using ARC while creating my own delegate, error regarding using _weak在 IOS5 下,在创建我自己的委托时使用 ARC,关于使用 _weak 的错误
【发布时间】:2012-04-12 01:35:44
【问题描述】:

在我的 .h 文件中,我有这个:

@protocol ScanBookDelegate
- (void)MethodNameToCallBack:(NSArray *)s;
@end

@property (nonatomic, weak) id  <ScanBookDelegate> delegate; 

在我的 .m 文件中,我有:

@synthesize delegate;

我收到此错误:

Existing ivar 'delegate' for __weak property 'delegate' must be __weak

如何解决此错误?

当我从 JSON 结果返回数据时,我返回了 NSArray *

更新:

这是我的整个 .h 文件 -

#import <UIKit/UIKit.h>

@protocol ScanBookDelegate
- (void)MethodNameToCallBack:(NSArray *)s;
@end

@interface jabBookScan : NSObject
<NSURLConnectionDelegate, NSURLConnectionDataDelegate>
{
    NSURLConnection *internalConnection;
    NSMutableData *container;
    id <ScanBookDelegate> delegate;
}
- (id)initWithRequest:(NSURLRequest *)req;
- (void)start;
- (NSMutableData *) lookupBook:(NSString *) isbn;
- (void)fetchedData:(NSData *)responseData;
@property (nonatomic, copy) NSURLRequest *request;
@property (nonatomic, copy) void (^completionBlock)(id obj, NSError *err);
@property (nonatomic, strong) NSDictionary *jsonRootObject;
@property NSMutableData *responseData;
@property NSURL *myURL;
@property (nonatomic, weak) id  <ScanBookDelegate> delegate; 
@end

【问题讨论】:

  • 您是否已经在其他地方定义了委托,比如在超类中?
  • @atticus - 我的印象是协议正在定义委托。上面的.h和.m文件是同一个类的,因为我在属性中引用了协议,然后尝试合成,然后报错。
  • 这听起来像是错误暗示已将 ivar 创建为 __strong,并且将属性声明为“弱”是有问题的。您介意发布整个 .h 文件吗?
  • @atticus - 我回家后会这样做,但这些行是唯一使用委托的地方。
  • 菲利普称之为。我怀疑你可能也在定义 ivar。

标签: ios5 delegates automatic-ref-counting


【解决方案1】:

在您的 {}(顶部)中删除 id &lt;ScanBookDelegate&gt; delegate;,它应该可以工作。使用属性时无需指定。如果你真的想要,那么你可以声明 ivar __weak 使其与你的属性相匹配(默认情况下 ivar 是强的)。

【讨论】:

    猜你喜欢
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多