Objective-C-无法与下划线Ivar合成的只读属性? [英] Objective-C - readonly properties not synthesized with underscore ivar?

查看:109
本文介绍了Objective-C-无法与下划线Ivar合成的只读属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正确理解,在Objective-C中,属性将自动与getter和setter进行合成,实例变量声明为带有下划线(_ivar)的属性名称.

If I understand correctly, in Objective-C, properties are automatically synthesized with getters and setters, with the instance variable declared as the property name with an underscore prepended (_ivar).

因此,此代码:

#import <Foundation/Foundation.h>
#import "hello.h"

int main(int argc, char *argv[])
{

    @autoreleasepool {

        Hello *hello = [[Hello alloc] init];
        NSLog(@"%@", hello.myString);

        return 0;

    }

}

hello.h

#import <Foundation/Foundation.h>

@interface Hello : NSObject
@property (copy, nonatomic) NSString *myString;
@end

hello.m

#import "hello.h"

@implementation Hello

-(Hello *)init
{

    if (self = [super init]) {
        _myString = @"Hello";
    }

    return self;

}

-(NSString *)myString
{
    return [NSString stringWithFormat:@"%@ %@", _myString, @"World"];
}

@end

可以像这样编译和运行:

Can be compiled and run like this:

bash-3.2$ clang -framework Foundation main.m hello.m -o hello
bash-3.2$ ./hello
2013-05-27 13:20:39.738 hello[23320:707] Hello World

现在,当我将myString属性更改为只读时,如下所示:

Now when I change the myString property to readonly like this:

@property (readonly, copy, nonatomic) NSString *myString;

然后当我尝试编译时出现错误:

Then when I try to compile I get an error:

hello.m:11:9: error: unknown type name '_myString'; did you mean 'NSString'?
        _myString = @"Hello";
        ^~~~~~~~~
        NSString

因此未定义_myString.编译器是否没有使用实例变量_myString来合成属性?让我们看看当我自己合成它时是否起作用:

So _myString is not defined. Did the compiler not synthesize the property with instance variable _myString? Let's see if it works when I synthesize it myself:

在hello.m实现中:

In hello.m implementation:

@synthesize myString = _myString;

现在它又可以工作了:

bash-3.2$ clang -framework Foundation main.m hello.m -o hello
bash-3.2$ ./hello
2013-05-27 13:36:59.916 hello[24219:707] Hello World

所以,我的问题是,为什么在属性上使用readonly时,它不会自动与下划线ivar合成?还是我完全错误地理解了Objective-C的工作原理?

So, my question is, why is it not automatically synthesized with an underscore ivar when you use readonly on properties? Or am I totally on the wrong path of understanding how this Objective-C stuff works?

我非常感谢您提供解释性的答案,因为我真的想了解发生了什么以及为什么.

I would very much appreciate an explaining answer, as I really want to understand what's exactly happening and why.

谢谢.

推荐答案

如果您实现所有必需的访问器方法(获取读取的内容,唯一的属性,一个读写属性的getter + setter).

A property is not automatically synthesized if you implement all required accessor methods (getter for a read-only property, getter + setter for a read-write property).

因为您为只读属性实现了getter方法-(NSString *)myString, 它不是自动合成的.如果您的getter需要实例变量来备份属性值,则必须添加synthesize语句(如您所做的那样)或实例变量.

Because you implement the getter method -(NSString *)myString for the read-only property, it is not auto-synthesized. If your getter needs an instance variable to backup the property value, you have to add the synthesize statement (as you did) or an instance variable.

这篇关于Objective-C-无法与下划线Ivar合成的只读属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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