I have the following code:
// MyObject.h
#import <Foundation/Foundation.h>
@interface MyObject : NSObject
@property (nonatomic, readonly) id property;
@end
// MyObject.m
#import "MyObject.h"
@interface MyObject ()
@property (nonatomic, copy, readwrite) id property;
@end
@implementation MyObject
@synthesize property = _property;
@end
这将生成以下编译器警告和错误:
warning: property attribute in continuation class does not match the primary class
@property (nonatomic, copy, readwrite) id property;
^
note: property declared here
@property (nonatomic, readonly) id property;
^
error: ARC forbids synthesizing a property of an Objective-C object with unspecified ownership or storage attribute
然而,如果我将类扩展的属性重新声明为
weak
存储限定符,就不会产生任何警告或错误。然而,令人惊讶的是,-[MyObject setProperty:]
生成的代码调用了objc_storeStrong
而不是我预期的objc_storeWeak
。我知道从LLVM 3.1开始,合成的实例变量默认的存储方式是
strong
。我的问题是:为什么代码生成器更喜欢头文件中的声明而不是我在实现中重新声明的呢?其次,为什么当我重新声明为copy
时会报错,但没有使用weak
或assign
时却不会报错?