属性使用

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

Properties Are Atomic by Default

By default, an Objective-C property is atomic:

atomic:实现与合成是私有化的

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.    系统的非容器类对象

这里指的是NSString,NSNumber等等一类的对象。

NSString *string = @"origion";

NSString *stringCopy = [string copy];

NSMutableString *stringMCopy = [string mutableCopy];

[stringMCopy appendString:@"!!"];

查看内存可以发现,string和stringCopy指向的是同一块内存区域(又叫apple弱引用weak reference),此时stringCopy的引用计数和string的一样都为2。而stringMCopy则是我们所说的真正意义上的复制,系统为其分配了新内存,但指针所指向的字符串还是和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对象,所以上述会报错。

对于系统的非容器类对象,我们可以认为,如果对一不可变对象复制,copy是指针复制(浅拷贝)和mutableCopy就是对象复制(深拷贝)。如果是对可变对象复制,都是深拷贝,但是copy返回的对象是不可变的。

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



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

你可能感兴趣的:(属性使用)