屬性使用

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

Properties Are Atomic by Default

By default, an Objective-C property is atomic:

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

implementation and synchronization

nonatomic:

it’s fine to combine a synthesized setter


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

抽象類(lèi)----分配初始化---對(duì)象----實(shí)例對(duì)象

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就是復(fù)制了一個(gè)imutable的對(duì)象,而mutablecopy就是復(fù)制了一個(gè)mutable的對(duì)象。以下將舉幾個(gè)例子來(lái)說(shuō)明。

1.? ? 系統(tǒng)的非容器類(lèi)對(duì)象

這里指的是NSString,NSNumber等等一類(lèi)的對(duì)象。

NSString *string = @"origion";

NSString *stringCopy = [string copy];

NSMutableString *stringMCopy = [string mutableCopy];

[stringMCopy appendString:@"!!"];

查看內(nèi)存可以發(fā)現(xiàn),string和stringCopy指向的是同一塊內(nèi)存區(qū)域(又叫apple弱引用weak reference),此時(shí)stringCopy的引用計(jì)數(shù)和string的一樣都為2。而stringMCopy則是我們所說(shuō)的真正意義上的復(fù)制,系統(tǒng)為其分配了新內(nèi)存,但指針?biāo)赶虻淖址€是和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:@"!!"];

以上四個(gè)NSString對(duì)象所分配的內(nèi)存都是不一樣的。但是對(duì)于mStringCopy其實(shí)是個(gè)imutable對(duì)象,所以上述會(huì)報(bào)錯(cuò)。

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

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



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

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

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容