--------------------------------
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返回的對象是不可變的。
--------------------------------
--------------------------------