佚名通过本文主要向大家介绍了property,property是什么意思,property line,propertymanager,system.getproperty等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题:property是什么意思 关于@property自动生成私有成员变量问题
描述:
解决方案1:
描述:
我现在知道@property int age;
编译器会自动生成属性, setter方法和getter方法的声明实现
int _age;
- (void)setAge:(int)age;
- (int)age;
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
但我用@property int _age;
生成的属性是什么? 不是__age 不是_age 也不是age
声明倒是没问题
- (void)set_age:(int)_age;
- (int)_age;
解决方案1:
哎...Xcode也是会时不时抽风的,我怀疑是Xc的问题, 我关了Xc, 再打开几个项目的时候, 错误信息居然在其他项目根本不相关的文件里...??
后来, 想起来用断点调试就可以看到里面的值了(新手,还没习惯用调试工具..)
果然像我想的一样
自动生成的私有属性是 int __age;
把它生成的所有东西写一下:
int __age;
- (void)set_age:(int)_age;
- (int)_age;
- (void)set_age:(int)_age
{
__age = _age;
}
- (int)_age
{
return __age;
}
应该没有写错吧??
解决方案2:@property int _age
这样定义属性默认是原子性(atomic)的,所以呢这时候set和get方法是不完整的,你需要进行使用@dynamic
或者@synthesize
来保证set和get同步,其实你只要换成@property (nonatomic)int age;
即可.与有没有'_'没有关系参考SO,中文版新浪博客