包含头文件的问题
Issues with including header files
在我的应用程序中,我有几个 classes,它们相互交叉。我需要连接这个 classes 并创建它们的属性,但是有些 classes 对另一个不可见,我想这里的主要问题是包含头文件。
class 我的操作队列
#import <Foundation/Foundation.h>
#import "ContentTableView.h"
//#import "CustomTableViewCell.h"
//#import "ObjectForTableCell.h"
@interface MyOperationQueue : NSOperation
@property(assign, nonatomic) BOOL isCancelled;
@property(strong, nonatomic) ContentTableView* tableView; //unknown type name
class ObjectForTableCell
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "MyOperationQueue.h"
@interface ObjectForTableCell : NSObject
@property (strong, nonatomic) MyOperationQueue* currentQueue;//unknown type name
class ContentTableView - 我这里没有警告
#import <UIKit/UIKit.h>
#import "CustomTableViewCell.h"
#import "Protocol.h"
#import "MyOperationQueue.h"
#import "ObjectForTableCell.h"
#import "ImageViewController.h"
@interface ContentTableView : UITableViewController<CellDelegate, NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate>
在
上方贴一个@class MyOperationQueue;
@interface ObjectForTableCell : NSObject
成功:
@class MyOperationQueue;
@interface ObjectForTableCell : NSObject
这里是官方文档的摘录here:
Referring to Other Classes
An interface file declares a class and, by importing its superclass,
implicitly contains declarations for all inherited classes, from
NSObject on down through its superclass. If the interface mentions
classes not in this hierarchy, it must import them explicitly or
declare them with the @class directive:
@class Rectangle, Circle;
This directive simply informs the
compiler that “Rectangle” and “Circle” are class names. It doesn’t
import their interface files. An interface file mentions class names
when it statically types instance variables, return values, and
arguments. For example, this declaration mentions the NSColor class.
Since declarations like this simply use the class name as a type and
don’t depend on any details of the class interface (its methods and
instance variables), the @class directive gives the compiler
sufficient forewarning of what to expect. However, where the interface
to a class is actually used (instances created, messages sent), the
class interface must be imported. Typically, an interface file uses
@class to declare classes, and the corresponding implementation file
imports their interfaces (since it will need to create instances of
those classes or send them messages).
The @class directive minimizes the amount of code seen by the compiler
and linker, and is therefore the simplest way to give a forward
declaration of a class name. Being simple, it avoids potential
problems that may come with importing files that import still other
files. For example, if one class declares a statically typed instance
variable of another class, and their two interface files import each
other, neither class may compile correctly.
还有一篇关于各种其他编译器指令的优秀文章(由 NSHipster 撰写)here。我建议您阅读它,因为这些知识将来可能会对您有所帮助。
有时,当您创建循环导入时,事情会变得一团糟,即 MyOperationQueue.h 和 ContentTableView.h 分别导入其他.
一个常见的修复方法是至少删除其中一个导入并用 类 的前向声明替换它们,即将 @class ContentTableView;
添加到 MyOperationQueue.h.
然后您应该在您的实现文件 MyOperationQueue.m 中导入 ContentTableView.h,以便了解整个接口。这通常也有助于减少编译时间。
在我的应用程序中,我有几个 classes,它们相互交叉。我需要连接这个 classes 并创建它们的属性,但是有些 classes 对另一个不可见,我想这里的主要问题是包含头文件。
class 我的操作队列
#import <Foundation/Foundation.h>
#import "ContentTableView.h"
//#import "CustomTableViewCell.h"
//#import "ObjectForTableCell.h"
@interface MyOperationQueue : NSOperation
@property(assign, nonatomic) BOOL isCancelled;
@property(strong, nonatomic) ContentTableView* tableView; //unknown type name
class ObjectForTableCell
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "MyOperationQueue.h"
@interface ObjectForTableCell : NSObject
@property (strong, nonatomic) MyOperationQueue* currentQueue;//unknown type name
class ContentTableView - 我这里没有警告
#import <UIKit/UIKit.h>
#import "CustomTableViewCell.h"
#import "Protocol.h"
#import "MyOperationQueue.h"
#import "ObjectForTableCell.h"
#import "ImageViewController.h"
@interface ContentTableView : UITableViewController<CellDelegate, NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate>
在
上方贴一个@class MyOperationQueue;
@interface ObjectForTableCell : NSObject
成功:
@class MyOperationQueue;
@interface ObjectForTableCell : NSObject
这里是官方文档的摘录here:
Referring to Other Classes
An interface file declares a class and, by importing its superclass, implicitly contains declarations for all inherited classes, from NSObject on down through its superclass. If the interface mentions classes not in this hierarchy, it must import them explicitly or declare them with the @class directive:
@class Rectangle, Circle;
This directive simply informs the compiler that “Rectangle” and “Circle” are class names. It doesn’t import their interface files. An interface file mentions class names when it statically types instance variables, return values, and arguments. For example, this declaration mentions the NSColor class.
Since declarations like this simply use the class name as a type and don’t depend on any details of the class interface (its methods and instance variables), the @class directive gives the compiler sufficient forewarning of what to expect. However, where the interface to a class is actually used (instances created, messages sent), the class interface must be imported. Typically, an interface file uses @class to declare classes, and the corresponding implementation file imports their interfaces (since it will need to create instances of those classes or send them messages).
The @class directive minimizes the amount of code seen by the compiler and linker, and is therefore the simplest way to give a forward declaration of a class name. Being simple, it avoids potential problems that may come with importing files that import still other files. For example, if one class declares a statically typed instance variable of another class, and their two interface files import each other, neither class may compile correctly.
还有一篇关于各种其他编译器指令的优秀文章(由 NSHipster 撰写)here。我建议您阅读它,因为这些知识将来可能会对您有所帮助。
有时,当您创建循环导入时,事情会变得一团糟,即 MyOperationQueue.h 和 ContentTableView.h 分别导入其他.
一个常见的修复方法是至少删除其中一个导入并用 类 的前向声明替换它们,即将 @class ContentTableView;
添加到 MyOperationQueue.h.
然后您应该在您的实现文件 MyOperationQueue.m 中导入 ContentTableView.h,以便了解整个接口。这通常也有助于减少编译时间。