在iOS上的Objective-C中,"self.foo"和"self.foo"之间的(样式)区别是什么?和"foo"什么时候使用合成吸气剂? [英] In Objective-C on iOS, what is the (style) difference between "self.foo" and "foo" when using synthesized getters?

查看:114
本文介绍了在iOS上的Objective-C中,"self.foo"和"self.foo"之间的(样式)区别是什么?和"foo"什么时候使用合成吸气剂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了关于ObjC访问器和综合访问器的许多问题,但均无济于事.这个问题更像是一个帮助我解决问题"的问题;我不希望有一个答案,但我想找专家考虑这个论点.

I have searched many questions on ObjC accessors and synthesized accessors to no avail. This question is more of a "help me settle an issue" question; I don't expect one answer, but I'm rather looking for experts to weigh in on the argument.

在Cocoa Touch类中,我会写一些这样的代码(其中soundEffects是一个合成的NSArray属性):

In a Cocoa Touch class, I would write some code like this (where soundEffects is a synthesized NSArray property):

id foo = [self.soundEffects objectAtIndex:1];

一位同事问我为什么上述内容比这行要好:

A colleague asked me to explain why the above is any better than this line:

id foo = [soundEffects objectAtIndex:1];

从功能上讲,没什么不同.

Well, functionally, it's no different.

我对前者的论据如下:

  1. self.soundEffects告诉处理该代码的所有其他编码器这是一个iVar,而不是局部范围的变量.

  1. self.soundEffects tells every other coder working on the code that this is an iVar, not a locally scoped variable.

如果需要,可以在soundEffects getter访问器中放入自定义逻辑.

If we ever needed to, we could put custom logic in the soundEffects getter accessor.

在没有具体原因的情况下,就像在Obj-C工作了一年之后要做的事情一样感觉".

For no concrete reason, it "feels" like the right thing to do after working in Obj-C for a year.

他接受参数#1和#2有效,但也给出了对点:

He accepts arguments #1 and #2 as valid, but also gives the counterpoint:

  1. 这不只是代码膨胀吗?

  1. Isn't this just code bloat?

是否应该允许类直接与其自己的iVar对话,而不必自己调用方法(getter)?

Shouldn't a class be allowed to talk to its own iVars directly without having to call a method (the getter) on itself?

有没有人?

推荐答案

您的观点1不太正确:self.soundEffects不是 一个ivar,尽管它可能碰巧会给您一些-就像您合成的NSArray一样,目前是 .

Your point 1 is not quite right: self.soundEffects is not an ivar, although it may happen to give you something which is -- as it does in the case of your synthesized NSArray, at the moment.

这反过来意味着第二点是问题的关键-如果您通过访问器路由所有访问,那么所有内容都被很好地封装了,您以后可以随意修改实现而不必担心副作用

This in turn implies that your point 2 is the crux of the matter -- if you route all access through the accessor, then everything is nicely encapsulated and you're free to modify the implementation later without having to worry about side effects.

这也是使用mutator的良好做法,因此您可以保持一致的内存管理.

It's also good practice for when you use the mutator, so you maintain consistent memory management.

在大多数情况下,我建议对所有 属性使用self.property进行路由,并限制直接ivar访问严格内部的内容.但是,我会承认,在某些情况下(尤其是对于不使用retain/copy语义的内容),它可能更多是样式首选项.

For the most part, I'd say it's advisable to route through self.property for everything that is a property, and restrict direct ivar access to things which are strictly internal. However, I'll admit that in some cases -- especially for things that don't use retain/copy semantics -- it can be more of a style preference.

这篇关于在iOS上的Objective-C中,"self.foo"和"self.foo"之间的(样式)区别是什么?和"foo"什么时候使用合成吸气剂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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