OS 2.2.1和OS 3之间的Objective-C更改? [英] Objective-C changes between OS 2.2.1 and OS 3?

查看:42
本文介绍了OS 2.2.1和OS 3之间的Objective-C更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试为OS 3编译我的应用程序时,遇到以下错误:

When I tried compiling my app for OS 3 I encountered an the following error:

错误:访问器类型与属性类型不匹配

error: type of accessor does not match the type of property

错误与我尝试访问的属性有关,该属性的定义如下:

The error was for a property I tried to access that is defined as follows:

NSMutableArray *myArray

@property (readonly,nonatomic) NSArray* myArray;

该属性在实现文件中@合成.

the property is @synthesized in the implementation file.

这在OS 2.2.1上工作正常,但在OS 3.0上却不是

This worked just fine in OS 2.2.1 but doesn't is OS 3.0

自己编写getter方法即可解决此问题.

Writing the getter method myself solved the problem.

有人知道OS 2.2.1和3.0之间对target-c的更改吗? 是否有任何有关这些更改的文档?

Is anyone aware of changes to objective-c between OS 2.2.1 and 3.0? Is there any documentation for these changes?

API更改文档似乎不包含与此问题有关的任何内容.

The API changes document doesn't appear to contain anything about this issue.

编辑

当您尝试访问属性时发生错误,例如

the error occurs when you try to access the property e.g.

NSArray *anArray = myClass.myArray;

如上所述,我发现了一个解决方法:自己编写getter方法,但是我真正追求的是苹果提供的某种文档,解释了此更改以及与API不相关的任何其他更改.

As I mentioned above I found a workaround for this: writing the getter method myself, however what I'm really after is some kind of documentation from apple explaining this change and any other changes that are not API related.

感谢您的帮助

推荐答案

这是一个编译器错误.

尽管您没有完全指定它,但我希望您的代码如下所示:

Though you didn't specify it completely, I expect your code looks like this:

@interface Foo : NSObject {
    NSMutableArray *objects;
}
@property (readonly, copy) NSArray *objects;
@end

@implementation Foo
@synthesize objects;
@end

不幸的是,编译器在objects 属性的声明和objects instance变量的声明之间感到困惑.记住,属性和实例变量在Objective-C中是不同的.一个属性可以由实例变量支持,但它实际上是类的公共接口的一部分.

The compiler is, unfortunately, confused between the declaration of the objects property and the declaration of the objects instance variable. Remember that properties and instance variables are different things in Objective-C; a property can be backed by an instance variable, but it's really part of the public interface of a class.

您可以通过更改代码以将实例变量的定义与属性的定义明确分开来解决此问题,例如,在实例变量的名称前加上前缀:

You can work around this by changing your code to clearly separate the definition of the instance variable from the definition of the property, for example by prefixing the name of the instance variable:

@interface Foo : NSObject {
    NSMutableArray *_objects;
}
@property (readonly, copy) NSArray *objects;
@end

@implementation Foo
@synthesize objects = _objects;
@end

通过这种方式,编译器不会对self.objects这样的表达式中的属性与实例变量感到困惑(无论如何,它显然不应该如此).

This way the compiler doesn't get confused about the property versus the instance variable in expressions like self.objects (which it shouldn't anyway, but apparently does).

只是为了避免不可避免的响应:Apple 不会为实例变量保留下划线前缀.它为方法保留.无论如何,如果您不喜欢下划线,请随时使用其他前缀.

Just to head off the inevitable response: Apple does not reserve the underbar prefix for instance variables. It's reserved for methods. Regardless, if you dislike the underbar, feel free to use another prefix.

这篇关于OS 2.2.1和OS 3之间的Objective-C更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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