升级到最新xcode,在使用AFNetworking时遇到了 property synthesis 相关的 error

將 Xcode 升级到最新,在使用AFNetworking时遇到了 property synthesis 相关的 error,错误信息如下:

Auto property synthesis will not synthesize property ‘request‘ because it is ‘readwrite‘ but it will be synthesized ‘readonly‘ via another property

Auto property synthesis will not synthesize property ‘response‘ because it is ‘readwrite‘ but it will be synthesized ‘readonly‘ via another property

在AFHTTPRequestOperation中定义了:

@property (readwrite, nonatomic, strong) NSURLRequest *request;

@property (readwrite, nonatomic, strong) NSHTTPURLResponse *response;

就是这样的代码,会让 request property 出现 warning。原因是因为 compiler 读取 sub-class 時,会发现 request 明明应该是個 readonly property(super-class 讲的),但你却要将它设为 read write property,所以 compiler 不知道该怎么 auto synthesis。

但你知道 super-class 的实现,也会将这个 property 改成 readwrite,因此你在 sub-class 的实现里这样子写是不会有问题的。可是 compiler 不知道啊,這要怎么办呢?

你要告诉 compiler,要它不用担心。那要怎么告诉 compiler 呢?你需要的是 @dynamic,它是一种给 compiler 的「承诺」,承诺它「虽然你现在不知道该怎么办,但是在 runtime 的时候你就会知道了」。所以只要把代码改成以下这样就可以了:

@implementation AFHTTPRequestOperation

@dynamic response;

@dynamic request;



@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值