Objective-C 声明的@property 属性(非原子、复制、强、弱) [英] Objective-C declared @property attributes (nonatomic, copy, strong, weak)

查看:20
本文介绍了Objective-C 声明的@property 属性(非原子、复制、强、弱)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我必须使用每个属性时,谁能给我详细解释一下:nonatomiccopystrongweak 等,用于声明的属性,并解释每个属性的作用?某种例子也会很棒.我正在使用 ARC.

Can someone explain to me in detail when I must use each attribute: nonatomic, copy, strong, weak, and so on, for a declared property, and explain what each does? Some sort of example would be great also. I am using ARC.

推荐答案

Nonatomic

非原子不会 通过 @synthesize 访问器生成线程安全例程.atomic 将生成线程安全访问器,因此 atomic 变量是线程安全的(可以从多个线程访问而不会损坏数据)

Nonatomic

Nonatomic will not generate threadsafe routines thru @synthesize accessors. atomic will generate threadsafe accessors so atomic variables are threadsafe (can be accessed from multiple threads without botching of data)

copy.如果您需要对象此时的值,并且您不希望该值反映该对象的其他所有者所做的任何更改,请使用此选项.完成后您需要释放对象,因为您保留了副本.

copy is required when the object is mutable. Use this if you need the value of the object as it is at this moment, and you don't want that value to reflect any changes made by other owners of the object. You will need to release the object when you are finished with it because you are retaining the copy.

Assigncopy 有点相反.当调用 assign 属性的 getter 时,它返回对实际数据的引用.通常,当您拥有原始类型的属性(float、int、BOOL...)时,您会使用此属性

Assign is somewhat the opposite to copy. When calling the getter of an assign property, it returns a reference to the actual data. Typically you use this attribute when you have a property of primitive type (float, int, BOOL...)

retain.分配应类似于:

NSObject* obj = [[NSObject alloc] init]; // ref counted var

@synthesize 生成的 setter 会在复制对象时为其添加引用计数,因此如果原始副本超出范围,底层对象不会自动销毁.

The setter generated by @synthesize will add a reference count to the object when it is copied so the underlying object is not autodestroyed if the original copy goes out of scope.

完成后,您需要释放对象.@propertys 使用 retain 会增加引用计数并占用自动释放池中的内存.

You will need to release the object when you are finished with it. @propertys using retain will increase the reference count and occupy memory in the autorelease pool.

strong 是保留属性的替代品,作为 Objective-C 自动引用计数 (ARC) 的一部分.在非 ARC 代码中,它只是保留的同义词.

strong is a replacement for the retain attribute, as part of Objective-C Automated Reference Counting (ARC). In non-ARC code it's just a synonym for retain.

这是一个了解 iOS 5 的 strongweak 的好网站.http://www.raywenderlich.com/5677/开始弧在 ios-5-part-1

This is a good website to learn about strong and weak for iOS 5. http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1

weakstrong 类似,只是它不会将引用计数增加 1.它不会成为该对象的所有者,而只是持有对它.如果对象的引用计数下降到 0,即使您可能仍然在此处指向它,它也会从内存中释放.

weak is similar to strong except that it won't increase the reference count by 1. It does not become an owner of that object but just holds a reference to it. If the object's reference count drops to 0, even though you may still be pointing to it here, it will be deallocated from memory.

上面的链接包含关于弱和强的好信息.

这篇关于Objective-C 声明的@property 属性(非原子、复制、强、弱)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆