objective-c中property的属性
来源:互联网 发布:python粒子群算法 编辑:程序博客网 时间:2024/06/04 19:59
Property:
是Objective-C 2.0中新增加的語法。利用這個語法,在precompiler時會幫我們產生getter及setter。
@property放在物件宣告的地方。基本語法如下:
@property(attributes, …) type propertyName;
Attributes間用”,”分開,可設定的屬性有:
1.getter,setter的名稱,如getter=getVaribleName
2.讀寫設定:
預設為readwrite,會產生getter與setter。
readonly表示沒有setter所以不能出現在等號的左邊。
3.setter設定
預設為assign,把等號右邊的數值與位址賦予等號左邊。
retain,只能用在objective-C的物件類型,在賦值的同時會呼叫retain,原本的值會被release釋放。
copy,用在Objective-C的物件類型,賦值時會自動呼叫copy,原本的值會被release釋放。此物件類型需實作NSCopying protocol。
assign与retain的区别:
There's no such thing as the "scope of an object" in Objective-C. Scope rules have nothing to do with an object's lifetime — the retain count is everything.
You usually need to claim ownership of your instance variables. See the Objective-C memory management rules. With a retain
property, your property setter claims ownership of the new value and relinquishes ownership of the old one. With an assign
property, the surrounding code has to do this, which is just as mess in terms of responsibilities and separation of concerns. The reason you would use an assign
property is in a case where you can't retain the value (such as non-object types like BOOL or NSRect) or when retaining it would cause unwanted side effects.
Incidentally, in the case of an NSString, the correct kind of property is usually copy
. That way it can't change out from under you if somebody passes in an NSMutableString (which is valid — itis a kind of NSString).
copy与retain的区别
copy是复制一个新对象给指针,retain是将原来的对象给指针。当需要修改值,但又不影响原来的值时,使用copy
copy的实验如下
代码:
NSMutableString *test = [[[NSMutableString alloc]initWithFormat: @"helloaaaa"]autorelease]; NSLog(@"1The test retaincount = %d", [test retainCount]); A *obj = [A new]; NSMutableString *a = [test copy]; NSRange r; r.length = 3; r.location = 2; [test replaceCharactersInRange:r withString:@"aaa"]; NSLog(@"2The test retaincount = %d, %@", [test retainCount], test); NSLog(@"3The a retaincount = %d, %@", [a retainCount], a); NSLog(@"After a release"); [a release]; NSLog(@"4The test retaincount = %d, %@", [test retainCount], test); NSLog(@"5The a retaincount = %d, %@", [a retainCount], a);
实验结果:
2010-08-12 21:28:38.135 TestRetain[1941:a0f] 1The test retaincount = 1
2010-08-12 21:28:38.138 TestRetain[1941:a0f] 2The test retaincount = 1, heaaaaaaa
2010-08-12 21:28:38.138 TestRetain[1941:a0f] 3The a retaincount = 1, helloaaaa
2010-08-12 21:28:38.139 TestRetain[1941:a0f] After a release
2010-08-12 21:28:38.139 TestRetain[1941:a0f] 4The test retaincount = 1, heaaaaaaa
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb)
4.getter設定
noatomic,預設是atomic(不寫就是用預設值,沒有atomic這個值),主要是用在多執行緒時,atomic保証可以取得正確的值,但效率較差。在手機上因效率考量,多用noatomic。
使用時在implementation中加入synthesize,語法如下
把build下及/Users/Jason/Library/Application Support/iPhone Simulator/4.0路径下缓存删了,重试
- objective-c中property的属性
- Objective-c 中@property中属性详解
- Objective-C中@property的所有属性详解
- Objective-C中@property的所有属性详解
- Objective-C中@property的所有属性详解
- objective-c中property 的属性strong,weak, retain, copy
- Objective-C中@property的所有属性详解
- iOS开发 Objective-C中@property的属性详解
- Objective-C中@property的所有属性详解
- Objective-C中@property的所有属性详解
- Objective-C中@property的所有属性详解
- Objective-C中@property所有的属性详解
- 动态实现Objective-C中属性(Property)的方法
- Objective-C中@property的所有属性详解
- Objective-C中@property的所有属性详解
- Objective-C中@property的所有属性详解
- Objective-C中@property的所有属性详解
- Objective-C中@property的所有属性详解【转】
- 重构时应避免过度思考
- hdu1597 find the nth digit(规律)
- 制作地图软件的经验和心得
- 不调用与 PostThreadMessage() PRB: MFC 消息处理程序
- object c 中 retain 和copy的区别
- objective-c中property的属性
- 变形PE头添加节形式感染
- ios捕获异常
- 2012.02.04梅林水库游玩
- PE详解之IMAGE_NT_HEADERS结构定义即各个属性的作用(PE详解02)
- 设计模式之Command模式(学习笔记)
- C++内联函数(Inline)介绍
- 数据结构基础(1)-->双向链表
- 重装系统的方式和原理