【问题标题】:Confused about properties and ivars对属性和 ivars 感到困惑
【发布时间】:2014-06-24 04:30:04
【问题描述】:

我意识到这里已经有很多关于财产与 ivar 的问题,但经过大量研究后,我似乎无法找到明确的答案。

我了解,当您声明如下属性时,编译器会自动为您合成支持 ivar 和两个访问器方法:

@property NSString *myString;

我仍然感到困惑的是,myString 是一个实际的实例变量吗?我问这个的原因是因为你永远不能像这样访问它:

NSLog(@"Value of myString is: %@", myString);

您要么必须使用支持 ivar _myString,要么必须使用一种 getter 方法,例如 [self myString]self.myString。所以我很困惑,因为通常你可以简单地使用变量名。

最重要的是,有人告诉我,您不应该将 myString 称为属性,并且属性一词只能用于指代由使用 @property 指令时的编译器。

您说“我有一个名为 myString 的属性”真的是错误的吗?如果这是错误的,那么正确的说法是什么?

任何帮助解决这个问题将不胜感激。我整天都在努力巩固属性和 ivars 是不同事物的想法。

【问题讨论】:

    标签: ios objective-c xcode properties


    【解决方案1】:

    这里是您问题的答案-

    is myString an actual instance variable? - No
    Would it really be wrong for you to say "I have a property called myString" , and if that is wrong then what would be the correct way to say it? - No its not wrong to call it.
    

    所以看起来真正让你感到困惑的是命名约定,如果你仔细阅读属性后面的命名约定 -

    When you use the @property syntax to declare properties on an object, as described in “Encapsulating Data,” the compiler automatically synthesizes the relevant getter and setter methods (unless you indicate otherwise). If you need to provide your own accessor method implementations for any reason, it’s important to make sure that you use the right method names for a property in order for your methods to be called through dot syntax, for example.

    Unless specified otherwise, a getter method should use the same name as the property. For a property called firstName, the accessor method should also be called firstName. The exception to this rule is for Boolean properties, for which the getter method should start with is. For a property called paused, for example, the getter method should be called isPaused.

    The setter method for a property should use the form setPropertyName:. For a property called firstName, the setter method should be called setFirstName:; for a Boolean property called paused, the setter method should be called setPaused:.

    查看开发者网站了解详细说明 - https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/Conventions/Conventions.html

    【讨论】:

    • 如果 myString 不是一个实际变量,那么为什么它们会让语法看起来就像你声明一个常规变量一样?这个不存在的 myString 变量是否与 _myString 的支持变量有关?我不知道它是什么,但这只是不适合我。
    • @property 关键字确保语法看起来不像声明常规变量。
    • 真的,如果你只有以下属性声明:@property NSString *myString;在这一点上,根本不存在变量吗?这就是为什么需要一个“支持”实例变量,以便在访问器方法实现中使用一个实际变量吗?
    • 那么当我定义像@property NSString *myString; 这样的属性时会发生什么?在这个编译器自动为我创建了 _myString 实例变量之后,我不必为此编写任何代码。我可以定义属性并以我喜欢的任何方式访问它 - self.myString 或 _myString。
    • 它实际上是一个实例变量,为你的类需要的数据保留内存。和 Property 只是为该实例变量创建 getter 和 setter。并且使用现代目标 c 创建属性将自动创建实例变量。
    【解决方案2】:

    我几乎是目标 c 的菜鸟,但我的理解如下:

    与任何其他 OOP 语言目标一样,c 具有实例 vars 和 getters+setters 方法。 基本上每个 getter 或 setter 看起来都一样,因此 XCode 让您可以使用 @property 语法自动合成它们。

    在 XCode 的早期版本中,您必须同时声明 ivar 及其 @property + @synthesize,但现在编译器会为您这样做。

    这段代码:

    @interface SomeClass : NSObject {
    NSString* _myInstanceVar; //declare the ivar
    }
    
    @property (non-atomic, strong) myInstanceVar //declare the property+accessors
    

    相当于这段代码:

    @property (non-atomic, strong) myInstanceVar //declare the ivar+property+accessors
    

    documentation 总结得差不多了。

    希望我能帮上忙……

    【讨论】:

      猜你喜欢
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      相关资源
      最近更新 更多