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:
我试图编译,但每次我都尝试编译,一个方法抛出一个奇怪的“期望类型”错误。我在header中有一个方法:
-(ANObject *)generateSomethingForSomethingElse:(NSString *)somethingElse;
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..
该方法的返回类型的错误点。我使用#import“ANObject”将ANObject导入到header中。而ANObject正在编译。
Why is this happening?
为什么会这样?
8 个解决方案
#1
90
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()
{
...
}
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.
这将导致编译时错误,因为还没有定义someMethod()。类也是如此。类由编译器在另一个后面编译。
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
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类还没有定义,所以我们还不能引用它。解决这个问题非常简单;更改编译顺序并编译。我敢肯定你在XCode里是这个屏幕的家庭成员:
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.
但是,您是否意识到您可以在列表中上下拖动文件?这改变了文件将被编译的顺序。因此,只要把船级的船级移到船厂的船级以上,就可以了。
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
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*类确实存在。这就是为什么@class关键字非常方便。
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
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.
现在编译器知道当它编译船坞时,一个Ship类定义很快就会出现。当然,如果没有,编译仍然会成功。
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关键字都会通知编译器该类将很快出现。它不是#import的替代品。您仍然必须导入头文件,否则您将无法访问任何类内部:
@class Ship
-(void) example
{
Ship* newShip = [[Ship alloc] init];
}
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.
这是不能工作的,并且会失败,错误消息说Ship是一个forward声明。一旦你#进口”船。然后,您将能够创建对象的实例。
#2
53
I found this error hapenning when there is circular dependency on the headers. Check if the .h file where you declare this method is imported in ANObject.h
当消息头出现循环依赖时,我发现了这个错误。检查您声明此方法的.h文件是否在ANObject.h中导入。
#3
25
You basically add
你基本上添加
@class ANObject;
before @interface!
@ interface之前!
#4
4
So, for some reason I was getting this error while trying to set a method with an enum type in the parameters. Like so:
因此,由于某些原因,我在尝试设置参数中带有枚举类型的方法时得到了这个错误。像这样:
- (void)foo:(MyEnumVariable)enumVariable;
I had previously used it like this and never had an issue but now I did. I checked for circular dependency and could find none. I also checked for typos multiple times and no dice. What ended up solving my issue was to adding 'enum' before I wanted to access the variable. Like so:
我以前就这样用过,从来没有出过问题,但现在我做到了。我检查了循环依赖项,却找不到。我也检查过多次,没有骰子。最终解决我的问题的是在我想要访问变量之前添加“enum”。像这样:
- (void)foo:(enum MyEnumVariable)enumVariable;
{
enum MyEnumVariable anotherEnumVariable;
}
#5
1
Usually when I see an error like this it's because I have a typo on a previous line, such as an extra or missing parenthesis or something.
通常当我看到这样的错误时,这是因为我在上一行有一个错误,比如一个额外的或缺少的圆括号之类的。
#6
1
It may sound stupid, but wrong shelling or wrong use of uppercase/lowercase letterwrong case this.
这可能听起来很愚蠢,但是错误的使用或错误的使用大写/小写字母错误的例子。
#7
1
I got this message, when the variable type was misspelled. See below this below
当变量类型拼写错误时,我得到了这个消息。请参见下面的下面
e.g.
如。
-(void)takeSimulatorSafePhotoWithPopoverFrame:(GCRect)popoverFrame {
instead of.....
而不是.....
-(void)takeSimulatorSafePhotoWithPopoverFrame:(CGRect)popoverFrame {
#8
0
Strangely enough, changing the order of my imports has fixed this in the past... Try moving the import to the bottom after all your other imports.
奇怪的是,改变我的进口订单已经解决了这个问题。在所有其他导入之后,尝试将导入移动到底部。
#1
90
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()
{
...
}
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.
这将导致编译时错误,因为还没有定义someMethod()。类也是如此。类由编译器在另一个后面编译。
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
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类还没有定义,所以我们还不能引用它。解决这个问题非常简单;更改编译顺序并编译。我敢肯定你在XCode里是这个屏幕的家庭成员:
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.
但是,您是否意识到您可以在列表中上下拖动文件?这改变了文件将被编译的顺序。因此,只要把船级的船级移到船厂的船级以上,就可以了。
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
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*类确实存在。这就是为什么@class关键字非常方便。
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
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.
现在编译器知道当它编译船坞时,一个Ship类定义很快就会出现。当然,如果没有,编译仍然会成功。
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关键字都会通知编译器该类将很快出现。它不是#import的替代品。您仍然必须导入头文件,否则您将无法访问任何类内部:
@class Ship
-(void) example
{
Ship* newShip = [[Ship alloc] init];
}
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.
这是不能工作的,并且会失败,错误消息说Ship是一个forward声明。一旦你#进口”船。然后,您将能够创建对象的实例。
#2
53
I found this error hapenning when there is circular dependency on the headers. Check if the .h file where you declare this method is imported in ANObject.h
当消息头出现循环依赖时,我发现了这个错误。检查您声明此方法的.h文件是否在ANObject.h中导入。
#3
25
You basically add
你基本上添加
@class ANObject;
before @interface!
@ interface之前!
#4
4
So, for some reason I was getting this error while trying to set a method with an enum type in the parameters. Like so:
因此,由于某些原因,我在尝试设置参数中带有枚举类型的方法时得到了这个错误。像这样:
- (void)foo:(MyEnumVariable)enumVariable;
I had previously used it like this and never had an issue but now I did. I checked for circular dependency and could find none. I also checked for typos multiple times and no dice. What ended up solving my issue was to adding 'enum' before I wanted to access the variable. Like so:
我以前就这样用过,从来没有出过问题,但现在我做到了。我检查了循环依赖项,却找不到。我也检查过多次,没有骰子。最终解决我的问题的是在我想要访问变量之前添加“enum”。像这样:
- (void)foo:(enum MyEnumVariable)enumVariable;
{
enum MyEnumVariable anotherEnumVariable;
}
#5
1
Usually when I see an error like this it's because I have a typo on a previous line, such as an extra or missing parenthesis or something.
通常当我看到这样的错误时,这是因为我在上一行有一个错误,比如一个额外的或缺少的圆括号之类的。
#6
1
It may sound stupid, but wrong shelling or wrong use of uppercase/lowercase letterwrong case this.
这可能听起来很愚蠢,但是错误的使用或错误的使用大写/小写字母错误的例子。
#7
1
I got this message, when the variable type was misspelled. See below this below
当变量类型拼写错误时,我得到了这个消息。请参见下面的下面
e.g.
如。
-(void)takeSimulatorSafePhotoWithPopoverFrame:(GCRect)popoverFrame {
instead of.....
而不是.....
-(void)takeSimulatorSafePhotoWithPopoverFrame:(CGRect)popoverFrame {
#8
0
Strangely enough, changing the order of my imports has fixed this in the past... Try moving the import to the bottom after all your other imports.
奇怪的是,改变我的进口订单已经解决了这个问题。在所有其他导入之后,尝试将导入移动到底部。