我是否应该在对象中使用@property(非原子,复制)或@property(非原子,强)作为我的(NSString *)attr? [英] Whether I should use @property(nonatomic,copy) or @property(nonatomic,strong) for my (NSString *) attr in An object?

查看:76
本文介绍了我是否应该在对象中使用@property(非原子,复制)或@property(非原子,强)作为我的(NSString *)attr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@interface PaneBean : NSObject

@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *type;
@property(nonatomic,assign) NSInteger width;
@end

我有一个如上所示的PaneBean。

I have a PaneBean as is shown above.

我是否应该使用 @property(非原子,复制) @property(非原子,强)我的(NSString *)名称?它们之间有什么区别?

Whether I should use @property(nonatomic,copy) or @property(nonatomic,strong) for my (NSString *) name? What is the difference between them?

为NSInteger写'assign'是对的吗?

And is it right to write 'assign' for NSInteger?

任何帮助赞赏。谢谢!

推荐答案

'copy'将导致该属性的setter创建对象的副本,并且在其他方​​面与强者相同。您可以使用它来确保如果有人将您的属性设置为可变字符串,然后改变字符串,您仍然具有原始值。如果字符串不可变,Cocoa将默默地优化复制操作,这很好:)

'copy' will cause the setter for that property to create a copy of the object, and is otherwise identical to strong. You would use this to make sure that if someone sets your property to a mutable string, then mutates the string, you still have the original value. If the string isn't mutable, Cocoa will silently optimize out the copy operation, which is nice :)

'strong'将使属性的值保持活动状态,直到它设置为别的。如果你希望传入的可变字符串从你下面改变(不是不可能的,但不是所有常见的,想要的东西),那么强者就是正确的做法。通常强对于表示比简单值更复杂的对象(即非NSString,NSNumber,NSValue等等)更有用。

'strong' will keep the property's value alive until it's set to something else. If you want incoming mutable strings to change out from under you (not impossible, but not all that common, a thing to want), then strong would be the right thing to do. Generally strong is more useful for objects that represent something more complex than a simple "value" (i.e. not NSString, NSNumber, NSValue, etc...).

'assign '是整数的默认设置(实际上只是)。无法像对象一样保留或复制整数。

'assign' is the default (and indeed only) possible setting for an integer. Integers can't be retained or copied like objects.

这篇关于我是否应该在对象中使用@property(非原子,复制)或@property(非原子,强)作为我的(NSString *)attr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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