我有以下代码:
// 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 开始,合成 ivars 的默认存储是strong
. 我想我的问题是这样的:为什么代码生成器偏爱标题中的声明而不是我在实现中的重新声明?其次,为什么当我重新声明为时它会抱怨copy
,而不是weak
or assign
?