objective-c中property的属性
2012-02-12 15:42
603 查看
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 [code]retainproperty, your property setter claims ownership of the new value and relinquishes ownership of the old one. With an
assignproperty, 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
assignproperty 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).[/code]
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 = 12010-08-12 21:28:38.138 TestRetain[1941:a0f] 2The test retaincount = 1, heaaaaaaa2010-08-12 21:28:38.138 TestRetain[1941:a0f] 3The a retaincount = 1, helloaaaa2010-08-12 21:28:38.139 TestRetain[1941:a0f] After a release2010-08-12 21:28:38.139 TestRetain[1941:a0f] 4The test retaincount = 1, heaaaaaaaProgram 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 2.0属性(Property) retain,copy,assign,copy
- Objective-C 基础知识之 (十四):属性 Property 点语法
- [Objective-C] property setter/getter @property属性
- Objective-C中的属性(property)
- iPhone SDK开发 objective C property属性详解
- Objective-C-类(static)方法、实例方法、overwrite(覆写)、属性(property)
- Objective-C 的OOP(下)-类(static)方法、实例方法、overwrite(覆写)、属性(property)
- Objective-C属性property详解
- Objective-C的属性(property)解析:
- Objective-C-类(static)方法、实例方法、overwrite(覆写)、属性(property)
- Objective-C 2.0属性(Property)介绍
- Objective-C 11 属性property
- iPhone SDK开发 objective C property属性详解
- Objective-C 2.0属性(Property)介绍
- 动态实现Objective-C中属性(Property)的方法
- Objective-C基础5 : 属性@property
- objective-c中property 的属性strong,weak, retain, copy
- 【iOS沉思录】深思Objective-C中的property属性
- Objective-C 学习笔记 11 - 属性Property
- Phone SDK开发 objective C property属性详解