使用ARC为ios声明只读属性的正确方法是什么 [英] What is the correct way to declare a readonly property for ios using ARC

查看:86
本文介绍了使用ARC为ios声明只读属性的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般来说,我是iOS开发的新手,从不处理手动引用计数(保留,发布,自动发布).因此,我对魔术ARC的性能没有很好的了解.

I am new to iOS development in general and have never dealt with manual reference counting (retain, release, autorelease). As such I don't have a good understanding of what magic ARC is performing.

我以为我理解了,直到被问到应该将什么类型的所有权(weakstrongassign等)赋予指向对象的只读属性,例如:

I thought I understood until I was asked what type of ownership (weak, strong, assign, etc) should be given to a readonly property pointing at an object, such as:

@property (readonly,nonatomic) NSString* name;

我在这里读 有关ARC中只读@property的问题,该问题忽略了/weak实际上不会编译,除非您在@synthesize属性中指定后备变量.我只是碰巧指定了这样的支持ivar:

I read here Questions about a readonly @property in ARC that leaving off the strong/weak won't actually compile unless you specify a backing variable when you @synthesize the property; I just so happened to be specifying a backing ivar like this:

@synthesize name = _name;

现在,我了解到变量的默认生存期限定符"很强,从这里开始:

Now I understand that the default 'lifetime qualifier' of a variable is strong, from here: http://developer.apple.com/library/ios/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226-CH1-SW4

因此,总而言之,我将我的属性间接定义为(readonly,nonatomic,strong),因为_name ivar被隐式声明为__strong.

So to cut a long story short - I am indirectly defining my property as (readonly,nonatomic,strong) as the _name ivar is implicitly declared as __strong.

我有几个问题:

  1. strong是否使用正确的寿命限定符?我以为是这样,否则支持我NSString*的对象将不会在任何地方拥有,因此将被自动释放(来自Java领域,这是有道理的,因为默认情况下所有引用都是强健的.)

  1. Is strong the correct lifetime qualifier to use? I assume that it is, otherwise the object backing my NSString* wouldn't be owned anywhere and would thus be freed automatically (coming from Java land this makes sense as all references are strong by default).

在这种情况下是否还有其他有意义的修饰符,例如copyassign?

Are there any other modifiers which make sense in this situation, such as copy or assign?

将属性声明为(readonly,nonatomic,strong)(readonly,nonatomic)是否与消费的代码有所不同?例如.不用strong关键字声明它会导致对象指针存储为__unsafe_unretained,而strong属性将存储在__strong指针中吗?

Does declaring the property as (readonly,nonatomic,strong) and (readonly,nonatomic) make any difference to the code which consumes the property? eg. does declaring it without the strong keyword cause the object pointer to be stored as __unsafe_unretained where the strong property would be stored in a __strong pointer?

谢谢!

编辑

据我所知,以下内容适用于只读属性:

So as I understand now, the following applies to readonly properties:

  • 对于非NSObject *类型(int,float,void *等),请使用(readonly, assign).
  • 对于对象指针,使用(readonly, strong)(readonly, copy)-这些功能对于只读属性相同,但是如果扩展/子类并将属性重新声明为readwrite,则可能需要复制语义.
  • 对于对象指针,(readonly, weak)仅在要在该属性中存储已经弱的指针时才有意义(该指针必须在其他位置强,否则该对象将被释放).
  • For non-NSObject* types (int, float, void*, etc) use (readonly, assign).
  • For object pointers, use (readonly, strong) or (readonly, copy) - these function the same for readonly properties but you may want the copy semantics if you extend/subclass and redeclare the property as readwrite.
  • For object pointers, (readonly, weak) only makes sense if you are going to be storing an already weak pointer in that property (that pointer must be strong elsewhere or the object will be deallocated).

推荐答案

    如果您要保留指向您所指对象的强大(拥有)引用,则使用
  1. strong是正确的.通常,您确实想要强引用,但是为了防止循环引用(尤其是在父母/孩子关系中,如果父母指向孩子,而孩子指向父母,则永远不会释放它们),有时您需要使用弱引用.另外,如果您想保留一个不属于您的对象的指针,但希望它仅在存在的情况下才有效 ,那么您想使用一个弱指针,因为当它由所有者释放后,您的指针将自动设置为nil,并且不会指向它不应该指向的内存.

  1. strong is correct to use if you want to keep a strong (owning) reference to whatever it is that you are pointing to. Usually, you do want strong, but in order to prevent circular references (particularly in parent/child relationships where if the parent points to the child and the child points to the parent, they will never be released) you sometimes need to use weak references. Also, if you want to keep a pointer to an object that you don't own but want it to be valid only as long as it exists, then you want to use a weak pointer because when it gets deallocated by the owner, your pointer will automatically get set to nil and won't be pointing to memory that it shouldn't be.

assign与标量值一起使用,并且是默认设置器.如果要自动创建对象的副本并将指针设置为副本,而不是指向原始对象,则copy很有意义.仅当您有特定需求时才这样做(通常是因为您不希望对象在您身上变异).

assign is used with scalar values, and is the default setter. copy makes sense if you want to automatically make a copy of the object and set your pointer to the copy instead of pointing to the original object. It only makes sense to do this if you have a specific need (usually because you don't want the object to mutate on you).

您提供的显示__strong为默认链接的链接(因此您无需指定它)指的是变量不是 声明的属性.声明的属性的默认值为assign,因此肯定会有所作为.但是,如果要使用assign,则无论是否指定它都没有什么区别(只是为了清楚地表明它是您想要的). 编辑:但是,正如Jacques所指出的那样,这在LLVM 3.1中正在发生变化,默认值为从assignstrong 更改 .在这种情况下,是否指定strong绝对没有区别,并且可以根据需要将其省略.我个人认为最好将其拼写出来(特别是因为不同版本之间存在冲突),以便每个查看代码的人都在同一页面上.但是其他人可能对此意见不一. :)

The link that you provided which shows that __strong is the default (and therefore you don't need to specify it) refers to variables and not to declared properties. The default for declared properties is assign so it certainly will make a difference. If you were wanting assign however, it makes no difference whether you specify it or not (other than just to be clear that it is what you wanted). EDIT: However, as Jacques pointed out, this is changing with LLVM 3.1 and the default is changing from assign to strong. In this case, it makes absolutely no difference whether or not you specify strong and can leave it out if you want. Personally I think that it is good to spell it out (especially since there is a conflict between different versions) so that everyone looking at the code is on the same page. Others may disagree on this point though. :)

我建议在以下位置阅读 Objective-C编程语言声明的属性部分.

I would suggest reading the Declared Properties section of The Objective-C Programming Language here: <document removed by Apple with no direct replacement>.

这篇关于使用ARC为ios声明只读属性的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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