“期望类型”错误指向方法的返回类型 [英] "Expected a type" error pointing to the return type of a method

查看:135
本文介绍了“期望类型”错误指向方法的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编译,但每次我做,一个方法抛出一个奇怪的预期一个类型的错误。我在头中有一个方法:

I've attempted to compile, but every time I do, one method throws a strange "expected a type" error. I have a method in the header:

-(ANObject *)generateSomethingForSomethingElse:(NSString *)somethingElse;

此方法的返回类型的错误点。我已经使用 #importANObject.h ANObject导入 ANObject

The error points at the return type for this method. I've imported ANObject into the header using #import "ANObject.h" and ANObject is compiling fine..

为什么会发生这种情况?

Why is this happening?

推荐答案

这与源文件编译的顺序有关。您可能已经知道在定义方法之前不能调用方法(见下面的伪代码):

This is to do with the order that the source files are compiled in. You are already probably aware that you can't call a method before it is defined (see below pseudocode):

var value = someMethod();

function someMethod()
{
    ...
}

这会导致编译时错误,因为someMethod()尚未定义。类同样如此。类由编译器依次编译。

This would cause a compile-time error because someMethod() has not yet been defined. The same is true of classes. Classes are compiled one after the other by the compiler.

所以,如果你想象所有的类在编译之前放入一个巨大的文件,你可能已经看到问题。让我们看看 BoatYard 类:

So, if you imagine all the classes being put into a giant file before compilation, you might be able to already see the issue. Let's look at the Ship and BoatYard class:

@interface BoatYard : NSObject
@property (nonatomic, retain) Ship* currentShip;
@end

@interface Ship : NSObject
@property (nonatomic, retain) NSString* name;
@property (nonatomic, assign) float weight;
@end

再次,因为 c $ c>类还没有定义,我们还不能引用它。解决这个特殊的问题很简单;更改编译顺序并编译。我确定你在XCode的这个屏幕是家族:

Once again, because the Ship class has not yet been defined, we can't refer to it yet. Solving this particular problem is pretty simple; change the compilation order and compile. I'm sure you're familliar with this screen in XCode:

但是你知道你可以在列表中上下拖动文件吗?因此,只需将 Ship 类移到 BoatYard 类之上,一切都很好。

But are you aware that you can drag the files up and down in the list? This changes the order that the files will be compiled in. Therefore, just move the Ship class above the BoatYard class, and all is good.

但是,如果你不想这样做,或者更重要的是,如果两个对象之间有一个循环关系怎么办?让我们通过添加对当前 BoatYard 的引用来增加对象图的复杂性: Ship 位于:

But, what if you don't want to do that, or more importantly, what if there is a circular relationship between the two objects? Let's increase the complexity of that object diagram by adding a reference to the current BoatYard that the Ship is in:

@interface BoatYard : NSObject
@property (nonatomic, retain) Ship* currentShip;
@end

@interface Ship : NSObject
@property (nonatomic, retain) BoatYard* currentBoatYard;
@property (nonatomic, retain) NSString* name;
@property (nonatomic, assign) float weight;
@end

哦,亲爱的,现在我们有一个问题。这两个不能并行编译。我们需要一种方法来通知编译器Ship *类确实存在。这就是为什么 @class 关键字很方便。

Oh dear, now we have a problem. These two can't be compiled side-by-side. We need a way to inform the compiler that the Ship* class really does exist. And this is why the @class keyword is so handy.

为了用俗语来说,你是说:相信我,船舶真的存在,你会很快看到它。一起来说:

To put it in layman's terms, you're saying, "Trust me man, Ship really does exist, and you'll see it really soon". To put it all together:

@class Ship;

@interface BoatYard : NSObject
@property (nonatomic, retain) Ship* currentShip;
@end

@interface Ship : NSObject
@property (nonatomic, retain) BoatYard* currentBoatYard;
@property (nonatomic, retain) NSString* name;
@property (nonatomic, assign) float weight;
@end

现在编译器知道它编译 BoatYard ,即将出现 Ship 类定义。

Now the compiler knows as it compiles BoatYard, that a Ship class definition will soon appear. Of course, if it doesn't, the compilation will still succeed.

然而,所有 @class 关键字都确实会成功。通知编译器该类很快就会出现。 替代 #import 。您仍然必须导入头文件,或者您将无法访问任何类的内部:

All the @class keyword does however is inform the compiler that the class will soon come along. It is not a replacement for #import. You still must import the header file, or you will not have access to any of the class internals:

@class Ship

-(void) example
{
    Ship* newShip = [[Ship alloc] init];
}

这不能工作,并且会失败,并显示一条错误消息,前瞻性声明。一旦你 #importShip.h,那么你将能够创建对象的实例。

This cannot work, and will fail with an error message saying that Ship is a forward declaration. Once you #import "Ship.h", then you will be able to create the instance of the object.

这篇关于“期望类型”错误指向方法的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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