屬性使用

--------------------------------

Properties Are Atomic by Default

By default, an Objective-C property is atomic:

atomic:實現(xiàn)與合成是私有化的

implementation and synchronization

nonatomic:

it’s fine to combine a synthesized setter


--------------------------------

抽象類----分配初始化---對象----實例對象

how those properties are implemented by default through synthesis of accessor methods and instance variables

If a property is backed by an instance variable, that variable must be set correctly in any initialization methods.

If an object needs to maintain a link to another object through a property, it’s important to consider the nature of the relationship between the two objects.

Properties? :Access to an Object’s Values、

Data Accessibility and Storage Considerations

An instance variable is a variable that exists and holds its value for the life of the object.

a readwrite property will be backed by an instance variable, which will again be synthesized automatically by the compiler.

You Can Define Instance Variables without Properties

Customize Synthesized Instance Variable Names

Most Properties Are Backed by Instance Variables

- (id)init {

return [self initWithFirstName:@"John" lastName:@"Doe" dateOfBirth:nil];

}

- (id)initWithFirstName:(NSString *)aFirstName lastName:(NSString *)aLastName {

return [self initWithFirstName:aFirstName lastName:aLastName dateOfBirth:nil];

}

- (id)initWithFirstName:(NSString *)aFirstName lastName:(NSString *)aLastName

dateOfBirth:(NSDate *)aDOB;

--------------------------------


Ios提供了copy和mutablecopy方法,顧名思義,copy就是復制了一個imutable的對象,而mutablecopy就是復制了一個mutable的對象。以下將舉幾個例子來說明。

1.? ? 系統(tǒng)的非容器類對象

這里指的是NSString,NSNumber等等一類的對象。

NSString *string = @"origion";

NSString *stringCopy = [string copy];

NSMutableString *stringMCopy = [string mutableCopy];

[stringMCopy appendString:@"!!"];

查看內存可以發(fā)現(xiàn),string和stringCopy指向的是同一塊內存區(qū)域(又叫apple弱引用weak reference),此時stringCopy的引用計數(shù)和string的一樣都為2。而stringMCopy則是我們所說的真正意義上的復制,系統(tǒng)為其分配了新內存,但指針所指向的字符串還是和string所指的一樣。

再看下面的例子:

NSMutableString *string = [NSMutableString stringWithString: @"origion"];

NSString *stringCopy = [string copy];

NSMutableString *mStringCopy = [string copy];

NSMutableString *stringMCopy = [string mutableCopy];

[mStringCopy appendString:@"mm"];//error

[string appendString:@" origion!"];

[stringMCopy appendString:@"!!"];

以上四個NSString對象所分配的內存都是不一樣的。但是對于mStringCopy其實是個imutable對象,所以上述會報錯。

對于系統(tǒng)的非容器類對象,我們可以認為,如果對一不可變對象復制,copy是指針復制(淺拷貝)和mutableCopy就是對象復制(深拷貝)。如果是對可變對象復制,都是深拷貝,但是copy返回的對象是不可變的。

--------------------------------



--------------------------------

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • @synthesize和@dynamic分別有什么作用?@property有兩個對應的詞,一個是 @synthes...
    筆筆請求閱讀 643評論 0 1
  • 1.什么是鏈式(函數(shù)式)編程? 通過高階函數(shù)以點為連接將多個函數(shù)連接在一起完成參數(shù)傳遞和復雜的操作! 例如在Mas...
    aaa000閱讀 3,791評論 3 14
  • 今天老婆和兒子沒有在家,很久很久沒有到電影院看過電影的我,聽兄弟介紹了吳京自導自演的《戰(zhàn)狼》二非常好看、也很值...
    靜遠庭閱讀 717評論 1 5
  • 三年前我回到了家鄉(xiāng)——大山的大山里面教書,我有一個宏偉計劃,就是家訪每一個學生,然后我就走遍了家鄉(xiāng)的每一個角落啦!...
    木風印閱讀 393評論 1 2
  • 老子日:“天地不仁,以萬物為芻狗;圣人不仁,以百姓為芻狗”。意思是說:干涉別人讓別人少碰壁其實是剝奪了別人成長的機...
    蘭博士心身靈閱讀 510評論 0 1

友情鏈接更多精彩內容