简体   繁体   English

内部属性与ivars

[英]Internal properties versus ivars

When I need a private object I currently use properties, like so: 当我需要私有对象时,我目前使用属性,如下所示:

// Class extension in .m file
@interface MyClass()
@property (strong, nonatomic) NSArray* myInternalArray;
@end

self.myInternalArray = something; 

Alternatively you can do this: 或者你可以这样做:

@implementation MyClass {

    NSArray* _myInternalArray;
}

_myInternalArray = something;

Without a custom setter or getter the two are equivalent. 没有自定义setter或getter,两者是等价的。 What is the best practice for internal variables? 内部变量的最佳实践是什么? Are there any advantages of one method over the other? 一种方法比另一种方法有什么优势吗?

While some may argue that the choice is a matter of preference, and they do have a point, there is a very good reason that most modern languages support properties and make them easier and easier to code. 虽然有些人可能认为选择是一个偏好问题,并且他们确实有一个观点,但是有一个很好的理由,即大多数现代语言都支持属性并使它们更容易和更容易编码。

The introduction of ARC does not significantly reduce the value of properties. ARC的引入并没有显着降低属性的价值。 It all comes down to this - in a property you have encapsulated the use of a variable. 这一切都归结为这一点 - 在一个属性中,你已经封装了变量的使用。 That encapsulation is invaluable when needed, and not much overhead when it is not. 这种封装在需要时是非常宝贵的,而在不需要时则没有多少开销。

For example (off of the top of my head) Suppose you discovered that you needed to validate the value before saving it. 例如(从头顶开始)假设您发现需要在保存之前验证该值。 If you were using an iVar, you would have to ensure that anywhere that iVar was used, you had a call the validation code before you allowed it's value to be changed. 如果您使用的是iVar,则必须确保在使用iVar的任何地方,您必须先调用验证代码,然后才能更改其值。 With a property, you would only need to override setIVarName: and put the validation there. 使用属性,您只需要覆盖setIVarName:并将验证放在那里。 One could argue that one is just as easy as the other - and that may be true in many cases, but there is one handicap with the iVar here - you cannot ensure that future changes (by you or other coders) will insert the validation before the iVar is changed. 有人可能会说,一个人就像另一个人一样容易 - 在许多情况下可能都是这样,但iVar在这里有一个障碍 - 你无法确保未来的变化(由你或其他程序员)将在之前插入验证iVar改变了。 Using a property here does have that assurance. 在这里使用房产确实有这种保证。

Personally, I use properties over iVars where ever possible. 就个人而言,我尽可能使用iVars上的属性。

I'd say that the advantage of properties is that you would use setters, and that setters can evolve independently of the code that call them. 我会说,属性的优点是你可以使用setter,并且setter可以独立于调用它们的代码而发展。 For instance, you could decide that setting a property would now trigger setNeedsLayout. 例如,您可以决定设置属性现在会触发setNeedsLayout。 By using properties from the start, you would have no need to refactor existing code. 通过从头开始使用属性,您无需重构现有代码。

This pattern fits very well in Cocoa/iOS APIs, where you don't have to ask system objects to do anything after having changed their properties: setters ensure internal and UI consistency right away. 这种模式非常适合Cocoa / iOS API,在更改其属性后,您无需要求系统对象执行任何操作:setters可立即确保内部和UI一致性。

The fact that properties are private should not make us implement them as second-class properties, what do you think? 属性是私有的这一事实不应该让我们将它们实现为二等属性,你怎么看?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM