OC 中 覆盖属性会有提示Auto property synthesis will not synthesize property 'delegate'

2 篇文章 0 订阅

在编写一个自定义相册的功能的时候,由于需要继承 UINavigationController写一个ImagePickerViewController,同时ImagePickerViewController又需要和其他的类进行沟通,所以很自然的,ImagePickerViewController类就需要有一个delegate属性,而且需要要求这个属性是一个实现了自定义的ImagePickerViewControllerDelegate协议的属性,那么这个时候问题就来了:父类是已经有一个这样的属性的。直接这么写编译器就会给出一个 warning

Auto property synthesis will not synthesize property 'delegate'; it will be implemented by its superclass, use @dynamic to acknowledge intention

诚然我们可以通过声明一个类似delegateObj的属性来避开这个问题,不过为何编译器会给出这个 warning 却值得研究。

要想研究这个问题首先得从什么是属性开始讲起,在 oc 中属性就是给一个类的成员变量提供封装:

Objective-C properties offer a way to define the information that a class is intended to encapsulate。

通过声明属性,我们可以很简单的为一个成员变量定义其是否是只读的还是读写的,是否是原子操作的等等特性,也就是说如果说封装是为成员变量套了一层壳的话,那么 @property关键字做的事情就是预定义这层壳是个什么样子的壳,然后通过 @sythesize关键字生成真正的壳并把这个壳套在实际的成员变量上(如果没有定义这个成员变量该关键字也可以自动生成对应的成员变量)。当然这层壳包括了自动生成的 get set 方法。
在最开始的时候,我们在代码中写了@property对应的就要写一个@sythesize,在苹果使用了 LLVM 作为编译器以后,如果我们没有写 @sythesize,编译器就会为我们自动的生成一个 @sythesize property = _property。这个特性叫做Auto property synthesize

说了这么多,现在我们来回头看看问题的关键,当我们想覆盖父类的属性并做一些修改的时候,Auto property synthesize这个特性就有点不知道该干嘛了,这个时候他选择不跑出来为我们干活,所以编译器就不会自动生成@sythesize property = _property,但是子类总得有个壳啊,人家都有@property了,怎么办?直接拿过来父类的壳复制一份不管三七二十一套在子类的成员变量身上。注意,有些情况下这会产生运行时的 crash,比如:
一个父类 A

@interface A : NSObject
@property(strong,nonatomic,readonly)NSString *name;

子类 Aa

@interface Aa : A
@property(strong,nonatomic,readwrite)NSString *name;

这种情况下编译器会给出 warning:

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

注意,虽然只给出了 warning,但是这个时候显然 Aa 中是不会自动生成 set 方法的,如果在代码中调用了 Aa 的实例对象的 set 方法,运行时就会 crash,crash 原因是:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Aa setName:]: unrecognized selector sent to instance

所以遇到这个问题怎么解决?在子类中显式的声明一个@synthesize name = _name;就好,这样子类就会如愿的产生他的壳,编译器也不纠结了,就去掉了 warning,从此,天下太平~

转载地址:http://segmentfault.com/a/1190000002904657

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值