属性使用

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

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

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



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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • @synthesize和@dynamic分别有什么作用?@property有两个对应的词,一个是 @synthes...
    笔笔请求阅读 532评论 0 1
  • 1.什么是链式(函数式)编程? 通过高阶函数以点为连接将多个函数连接在一起完成参数传递和复杂的操作! 例如在Mas...
    aaa000阅读 3,732评论 3 14
  • 今天老婆和儿子没有在家,很久很久没有到电影院看过电影的我,听兄弟介绍了吴京自导自演的《战狼》二非常好看、也很值...
    静远庭阅读 612评论 1 5
  • 三年前我回到了家乡——大山的大山里面教书,我有一个宏伟计划,就是家访每一个学生,然后我就走遍了家乡的每一个角落啦!...
    木风印阅读 312评论 1 2
  • 老子日:“天地不仁,以万物为刍狗;圣人不仁,以百姓为刍狗”。意思是说:干涉别人让别人少碰壁其实是剥夺了别人成长的机...
    蘭博士心身灵阅读 445评论 0 1