xcode Objective-C #import 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1223914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C #import loop
提问by tba
I have the following code:
我有以下代码:
#import <Foundation/Foundation.h>
#import "ServerRequest.h" // works even though this line is included
#import "ServerResponseRecord.h"
@protocol ServerRequestDelegate<NSObject>
-(void)request:(id)request gotResponseRecord:(ServerResponseRecord*)response;
-(void)request:(id)request gotError:(NSError*)error;
@end
It compiles and runs fine. However, if I replace the method declarations with:
它编译并运行良好。但是,如果我将方法声明替换为:
-(void)request:(ServerRequest*)request gotResponseRecord:(ServerResponseRecord*)response;
-(void)request:(ServerRequest*)request gotError:(NSError*)error;
I get the unexpected syntax error "error: expected ')' before 'ServerRequest'". The only reason I can think this might be a problem is that ServerRequestDelegate.h and ServerRequest.h #import each other. However, I don't understand why the code works with the #import line with (id)request. I also don't understand why it's a syntax error.
我在 'ServerRequest' 之前收到意外的语法错误“错误:预期的 ')'”。我认为这可能是一个问题的唯一原因是 ServerRequestDelegate.h 和 ServerRequest.h #import 彼此。但是,我不明白为什么代码可以与带有 (id) 请求的 #import 行一起使用。我也不明白为什么这是一个语法错误。
Can someone provide a good explanation?
有人可以提供一个很好的解释吗?
回答by Quinn Taylor
You've already hinted at the explanation: an #import cycle.
您已经暗示了解释:#import 循环。
The first thing I'd do is remove the #include
and add the following line above the @protocol
definition:
我要做的第一件事是删除#include
并在@protocol
定义上方添加以下行:
@class ServerRequest;
This is a forward class declaration, and can help break the import loop. Check out this SO questionfor more details. Apple also has a brief explanation in this guide.
这是一个前向类声明,可以帮助打破导入循环。查看此 SO 问题以获取更多详细信息。Apple 在本指南中也有简要说明。
Basically, #import
'ing a file causes the compiler to bring the entire text of that file into the file in question, and although #import
is "smarter" than #include
, it doesn't mean you're immune from import errors. The @class
declaration is a way to tell the compiler that a class exists without importing the header. It's appropriate to use when you only need to know about the class name, but don't care about the methods it provides. Generally, you want to use @class
in the .h file and #import
in the .m file, where you're actually interacting with the class.
基本上,#import
'ing 一个文件会导致编译器将该文件的整个文本带入有问题的文件中,尽管#import
比 '聪明' #include
,但这并不意味着您可以免受导入错误的影响。该@class
声明的方式来告诉编译器一类存在不导入头。当你只需要知道类名而不关心它提供的方法时使用它是合适的。通常,您希望@class
在 .h 文件和#import
.m 文件中使用,您实际上是在其中与类进行交互。
回答by Louis Gerbarg
#import "loops" are not a problem. #import is the same as #include except that it tracks files and makes sure the preprocessor only reads them in the first time.
#import“循环”不是问题。#import 与 #include 相同,不同之处在于它跟踪文件并确保预处理器仅在第一次读取它们。
Usually when you get an error like that it is due to a problem in the included file. So the error is probably in ServerResponseRecord.h, thought it is probably being tripped by actually using the object declared by it. Without seeing the complete headers it is not possible to say exactly what is going on.
通常,当您收到这样的错误时,这是由于包含的文件中存在问题。所以错误可能在ServerResponseRecord.h中,认为它可能是通过实际使用它声明的对象而被绊倒的。如果没有看到完整的标题,就不可能确切地说出发生了什么。