美文网首页
ios基础之属性set get方法同时重写报错的问题

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

作者: 流沙3333 | 来源:发表于2017-03-10 15:31 被阅读200次

    今天有一个初学者遇到一个问题,就是使用property的时候,同时重写set

    get方法会报错,如

    @interface ViewController : UIViewController{

    }

    @property (nonatomic, copy) NSString *name;

    - (void)setName:(NSString *)name;

    - (NSString *)name;

    @end

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

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

    这时就不会报错了。

    第二种办法: 在实现里面写 @synthesize name = _name;

    相关文章

      网友评论

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

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