IOS Intro
来源:互联网 发布:联排牙的利与弊 知乎 编辑:程序博客网 时间:2024/06/05 18:42
http://blog.csdn.net/ouy_huan/article/details/8959447
http://hi.baidu.com/feng20068123/item/ca8952fa661e5342932af2c2
写的非常不错,攒一个!!!!
Synthesized property 'xX' must either be named the same as a compatible ivar or must explicitly name an ivar
在 64-bit时,运行时系统会自动给类添加 ivar,添加的 ivar 以一个下划线"_"做前缀。
上面声明部分的 @synthesize window=_window; 意思是说,window 属性为 _window 实例变量合成访问器方法。
也就是说,window属性生成存取方法是setWindow,这个setWindow方法就是_window变量的存取方法,它操作的就是_window这个变量。通过这个看似是赋值的这样一个操作,我们可以在@synthesize 中定义与变量名不相同的getter和setter的命名,籍此来保护变量不会被不恰当的访问。
下面是一个常见的例子
写法一:
@interface MyClass:NSObject{
MyObjecct *_myObject;
}
@property(nonamtic, retain) MyObjecct *myObject;
@end
@implementatin MyClass
@synthesize myObject=_myObject;
@interface MyClass:NSObject{
}
@property(nonamtic, retain) MyObjecct *myObject;
@end
@implementatin MyClass
@synthesize myObject=_myObject;
self.nameVarPtr = [[ObjectName alloc] init]
nameVarPtr = [[ObjectName alloc] init]
self.nameVarPtr=xxx 这种赋值方式等价于调用 [self setnameVarPtr:xxx], 而setnameVarPtr:xxx的方法的实现又是依赖于@property的属性的,比如retain,assign等属性。
nameVarPtr = xxx 的 赋值方式,仅仅是对一个指针进行赋值。nameVarPtr仅仅是一个指针变量,记录了xxx的地址。在这个过程中不会调用setter方法,不会调用 setter方法,就和@property没有关系,从而,也和retain,assign等属性没有关系。这种赋值方式就是一个简单的指针赋值。
综上,对成员变量进行赋值,为防内存泄露需要注意的点:
1.self调用setter方法的方式
ObjectName* tmp= [[ObjectName alloc] init];
self.nameVarPtr =tmp; //retainCount=2
[tmp release]; //retainCount=1
2.指针赋值方式,不会调用setter方法
nameVarPtr= [[ObjectName alloc] init]; // retainCount=1
所以,笔者建议大家在对某个变量进行赋值操作的时候,尽量要写self.myObj = xxx; 这才是最可靠的方法。
坏的做法
@private
NSObject* myObj_;
}
@property(strong, nonatomic) NSObject* myObj;
@end
// …
@implementation Foo
@synthesize myObj = myObj_;
- @end
好的做法
@property(strong, nonatomic) NSObject* myObj;
@end
// …
@implementation Foo
@synthesize myObj = myObj_;
@end
You may still need to declare the underlying name of the variable if you need to access it directly, as when writing custom getters and setters:
当你自己在为实例变量写setter和getter的时候,如果你在其中需要直接访问变量的话,你得声明名字带下划线的变量:
错误的写法
@interface Foo : NSObject
@property(strong, nonatomic) NSObject* myObj;
@end
// …
@implementation Foo
@synthesize myObj;
- (NSObject*)myObj
{
return self.myObj; // 会递归调用getter!
}
- (void)setMyObj:(NSObject*)myObj
{
self.myObj = myObj; // 会递归调用setter!
}
@end
正确的写法
@interface Foo : NSObject
@property(strong, nonatomic) NSObject* myObj;
@end
// …
@implementation Foo
@synthesize myObj = myObj_;
- (NSObject*)myObj
{
return myObj_; // 没问题.
}
- (void)setMyObj:(NSObject*)myObj
{
// 没问题
myObj_ = myObj; // 进行赋值(ARC会处理必要的retain和release)
}
@end
解释的非常好 ,学习了
- IOS Intro
- IOS Intro
- IOS Dev Intro - UIControlEvent
- IOS Dev Intro - UIButton
- IOS Dev Intro - UILabel
- IOS Dev Intro - String
- IOS Dev Intro - FBO
- IOS Dev Intro - NSDictionary
- IOS Dev Intro - NSArray
- IOS Dev Intro - NSInteger
- IOS Dev Intro - NSString
- IOS Dev Intro - UISwitch
- IOS Dev Intro - Sandbox
- IOS Dev Intro - NSFileManager
- IOS Dev Intro - NSBundle
- IOS Dev Intro - NSSet
- IOS Dev Intro - Property
- IOS Dev Intro - Block
- Oracle Enterprise Manager打不开的解决方法
- JAVA ------SSH 面试题
- jQuery.extend() 函数详解
- Ubuntu设置交换分区
- 局域网git 克隆
- IOS Intro
- ABAP DATA 和 JSON之间的转换
- Unity3D优化技巧系列四
- AIDL笔记
- CSS 盒子模型
- 中间件
- 前端开发调试经验
- yii2 leftjoin 不显示子表的属性
- Maven 私有库和本地的安装与配置(一)