顯示具有 @property 標籤的文章。 顯示所有文章
顯示具有 @property 標籤的文章。 顯示所有文章

2014年10月16日 星期四

atomic vs. nonatomic

nonatomic :
  • does not enforce thread safety on the property, mainly for use when only one thread shall be used throughout a program.


atomic(default) : 我目前有找到幾種解釋,因為還沒整理完,所以先都貼上來。
  • enforces thread safety on the property, mainly for use when multiple threads shall be used throughout a program
  • More precisely, tomic properties do not ensure thread-safety; rather it ensures atomicity. If thread A and thread B are both writing, atomic ensures that the outcome will be a whole value, meaning either one or the other. Which one is undefined. Writing thread-safe code is not as simple as using atomic properties. See the "Synchronization" section of the Threading Programming Guide
  • atomic是Objc使用的一種線程保護技術,基本上來講,是防止在寫未完成的時候被另外一個線程讀取,造成數據錯誤。而這種機制是耗費系統資源的,所以在iPhone這種小型設備上,如果沒有使用多線程間的通訊編程,那麼 nonatomic 是一個非常好的選擇。
  • Atomicity has to do with how properties behave in a threaded environment. When you have more than one thread, it’s possible for the setter and the getter to be called at the same time. This means that the getter/setter can be interrupted by another operation, possibly resulting in corrupted data. Atomic properties lock the underlying object to prevent this from happening, guaranteeing that the get or set operation is working with a complete value. However, it’s important to understand that this is only one aspect of thread-safety—using atomic properties does not necessarily mean that your code is thread-safe.

整理來源:
http://stackoverflow.com/a/10753201/3295047
http://stackoverflow.com/questions/9162926/ios-property-declaration-clarification
http://rypress.com/tutorials/objective-c/properties.html

2014年10月9日 星期四

@property 介紹

@property是在Objective-C 2.0(2006年)新加入的功能,目的是要簡化一個實體變數(instance variable, 簡稱ivar)設定的過程。
在沒有@property功能之前,要自己宣告setter跟getter。

各種屬性介紹

讀寫相關:
  • readwrite(default)
  • readonly

記憶體管理相關:
  • Automatic Reference Counting(ARC):這部分對初學者來說比較難懂,簡單講,絕大部分時我們會用的是strong,weak是一些比較進階的功能才會用到,所以初學者每次在設定的時候用strong就可以了。如果想要知道背後詳細的原理及解釋可以看我另外寫的這篇Automatic Reference Counting(ARC)
    • strong:
    • weak:
  • Manual Reference Counting(MRC):這是上一代的記憶體管理方式,現在已經不用了,因為ARC遠遠比MRC好用太多了,只需要知道有這些屬性的存在就可以了,因為在網路上還是很容易找到比較舊的文章是用MRC。
    • assign(default) : 直接指派,不進行 retain
    • retain : 先 release 再 retain 新值
    • copy : 先 release 再 copy 新值

多執行緒相關:對初學者來說,這部分比較難懂,我後面打的解釋是結果論,如果想要知道兩個屬性背後實際上是怎麼回事可以看這篇atomic vs. nonatomic
  • nonatomic : 沒有要用多執行序就選這個,但不知道為什麼Apple並沒有把這個屬性設為預設,所以幾乎是每次都要加上這個屬性。
  • atomic(default) : 如果要使用多執行序就選這個。

setter/getter相關(accessors):
  • setter = setterName:重新命名setter名稱
  • getter = getterName:重新命名getter名稱,最常改的方式是在原本的名稱加「is」ex:@property (nonatomic, getter = isChosen) BOOL chosen;

@synthesize

@property DECLARES the accessors for your property, @synthesize will IMPLEMENT them.
原本是在@interface的程式
-(int) price;
-(void) setPrice: (int) p;
可以只要寫
@property int price;

在@implementation的程式從
-(int) price
{
  return price;
}

-(void) setPrice: (int) p
{
  price = p;
}
變成只要寫
@synthesize price;
Xcode 4.3 之前,每一個 @property 都要在 @implementation 加上 @synthesize 變數名稱 或 @synthesize 變數名稱 =_ 變數名稱 但 4.4 之後就自動化了,如果沒寫,就等於寫了 @synthesize 變數名稱 =_ 變數名稱,而 @synthesize 的設定,也可以改變變數的名稱,如 【@synthesize 變數名稱;】 時,變數名稱前面就不再有 underline, 有興趣的讀可以查一下 @synthesize 的功能。

2012年Objective-C加入了不少新功能,@synthesize變成預設值,現在有沒有寫@synthesize的差別只在於你取用變數的方式:
有寫@synthesize
price
沒有寫@synthesize
self.price

自訂setter/getter

但如果你要自訂自己的setter/getter的話,你就要自己寫setter/getter 函數,系統會以你自己寫的函數為主。
例如我們自訂setter:
-(void)setPrice:(int)p
{
  price = p * 2;
}
這樣的話當我們在給price值的時候,就會自動變成兩倍,也就是當我們打的程式碼是:
price = 3;
price的值實際上是6而不是3,因為3傳入時還會被乘以二。


Attribute 屬性

在2006年發佈了Objective-C 2.0後,Objective-C也可以使用「.」來存取物件的屬性
原本要設值是這樣
[b setPrice:300];
就可以寫成
b.price = 300;

不過在高見龍的部落格裡有提到點語法的一個小細節,有可能造成無窮回圈:
-(void) setPrice: (int) p
{
  self.price = p;
}
如果在自訂的setter裡使用了點語法,那就會不斷的呼叫自己(setter函數),變成無窮迴圈。


    整理來源:
    http://stackoverflow.com/questions/9162926/ios-property-declaration-clarification
    http://pydoing.blogspot.tw/2012/08/objc-property-and-synthesize.html
    http://shenfive.pixnet.net/blog/post/48054812-@property-
    http://blog.eddie.com.tw/2010/12/08/property-and-synthesize/