美文网首页iOS开发攻城狮的集散地oc基础面试题
ios基础之属性set get方法同时重写报错的问题

ios基础之属性set get方法同时重写报错的问题

作者: xywjun | 来源:发表于2016-03-21 16:23 被阅读8823次

今天有一个初学者遇到一个问题,就是使用property的时候,同时重写set get方法会报错,如
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

}
@property (nonatomic, copy) NSString *name;
- (void)setName:(NSString *)name;
- (NSString *)name;
@end

单独重写任意一个方法都不会报错,但是同时重写的话,会报错!

QQ20160321-2.png

主要是因为当你复写了get和set方法之后@property默认生成的@synthesize就不会起作用了,这也就意味着你的类不会自动生成出来实例变量了,你就必须要自己声明实例变量,如下:

QQ20160321-3.png QQ20160321-4.png

这时就不会报错了。

相关文章

网友评论

  • char_hu:谢谢!
  • 凯撒牛:谢谢 应该也可以在实现里面写@synthesize name = _name

本文标题:ios基础之属性set get方法同时重写报错的问题

本文链接:https://www.haomeiwen.com/subject/hbmklttx.html