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

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

问题描述

我曾尝试编译,但每次编译时,一个方法都会抛出一个奇怪的预期类型"错误.我在标题中有一个方法:

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;

错误指向此方法的返回类型.我已经使用 #import "ANObject.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..

为什么会这样?

推荐答案

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

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.

因此,如果您想象在编译之前将所有类放入一个巨大的文件中,您可能已经看到了这个问题.让我们看看 ShipBoatYard 类:

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

再一次,因为Ship 类还没有被定义,我们还不能引用它.解决这个特殊问题非常简单;更改编译顺序并编译.我相信您对 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.

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

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.

用外行的话来说,你是在说,相信我,Ship 确实存在,你很快就会看到它".把它们放在一起:

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];
}

这行不通,并且会失败并显示错误消息,指出 Ship 是前向声明.一旦你#import "Ship.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天全站免登陆