基础标头而不是 Objective-C 类(NSObject 子类)的 cocoa 标头
在 xcode 4 中,当我尝试使用 Mac OS X 模板创建一个类(例如“ABClass”)时,创建文件时的最终结果是:
//header
#import <Foundation/Foundation.h>
@interface DBFTimer : NSObject {
@private
}
@end
另一个文件
//.m file
#import "DBFTimer.h"
@implementation DBFTimer
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
@end
是一个错误吗?解决办法是什么? (运行 Xcode 4 Build 4A304a)
编辑:好的,现在我明白为什么了,因为这是 NSObject
的子类,因此只需要基础标头。
in xcode 4, when i try to create a class, for example "ABClass" using a template for Mac OS X, the end result when the file created is:
//header
#import <Foundation/Foundation.h>
@interface DBFTimer : NSObject {
@private
}
@end
and the other file
//.m file
#import "DBFTimer.h"
@implementation DBFTimer
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
@end
is this a bug? and what is the solution? (running Xcode 4 Build 4A304a)
EDIT: ok now i understand why, as this is an subclass of NSObject
, thus the foundation header only is required.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个对 Cocoa 和 Cocoa Touch 都有效的类。我相信最新版本的 Xcode 会根据您指定的超类来决定是否导入 Cocoa/Cocoa.h 还是 Foundation/Foundation.h。如果您创建的类继承自
NSObject
,则无需导入整个 Cocoa — 仅 Foundation 就足够了。That’s a valid class for both Cocoa and Cocoa Touch. I believe recent versions of Xcode decide whether to import Cocoa/Cocoa.h or Foundation/Foundation.h based on what you’ve specified as the superclass. If the class you’ve created inherits from
NSObject
, there’s no need to import the whole of Cocoa — Foundation alone suffices.您发布的示例是一个完全有效的 Mac OS X Cocoa 类。 (即:该类没有任何与 iOS/Cocoa Touch 相关的内容。)
就您使用
对 iOS 的评论而言 - 情况并非如此 - 如果您查看在
中,您会发现它实际上包含基础标头本身以及其他项目,例如 CoreData 等。The example you've posted is a perfectly valid Mac OS X Cocoa class. (i.e.: There's nothing about that class that's iOS/Cocoa Touch related.)
In terms of your comment about iOS using
<Foundation/Foundation.h>
- this isn't the case - if you look inside<Cocoa/Cocoa.h>
, you'll find that it actually includes the foundation header itself as well as other items such as CoreData, etc.