【问题标题】:Overriding property accessor names in Objective-C with @synthesize使用 @synthesize 覆盖 Objective-C 中的属性访问器名称
【发布时间】:2010-09-27 18:41:08
【问题描述】:

我正在尝试查找有关如何使用 @synthesize 覆盖 Objective-C 中的属性名称的文档。如果我有一个名为“foo”的实例变量,我想将它的访问器写为“bar”。

做一些事情,比如

@synthesize foo = bar;

给出编译时错误。

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    我认为你只是在你的@synthesize 中把它倒过来了。你想要的是:

    @synthesize bar = foo;
    

    【讨论】:

      【解决方案2】:

      您可以在声明属性时更改 getter 名称 (getter=bar)。

      【讨论】:

        【解决方案3】:

        你可以,但“bar”必须首先存在(作为声明的变量)。来自documentation

        您可以使用property=ivar 的形式来指示应该为属性使用特定的实例变量,例如:

        @synthesize firstName, lastName, age = yearsOld;
        

        【讨论】:

        • 在 OP 的示例中,正确的形式是 @synthesize bar = foo;因为他说 foo 是他的 ivar,他想用 bar 访问它。
        猜你喜欢
        • 1970-01-01
        • 2011-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-29
        • 2015-04-26
        • 1970-01-01
        • 2013-01-31
        相关资源
        最近更新 更多