foundation header instead of cocoa header for objective-c class (NSObject subclass)
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.
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.
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.
精彩评论