Reputation: 12149
I just noticed that when I create iPhone application using standard templates in Xcode (View Based Application, Window based application etc) the header files only import UIKit.h (and not Foundation.h). So if the Foundation.h file is not imported, how can one use Foundation classes like NSString, NSArray etc in the program ? Shouldn't it be throwing an error?
Upvotes: 4
Views: 6254
Reputation: 12988
It looks like the template files are designed to work with or without precompiled headers, so they import the header files they use. If you create a subclass of NSObject
the template file will import Foundation
. If you create a subclass of a UIKit
object the UIKit
header will be imported.
OK, so why can you use Foundation
classes like NSString
when only UIKit
is imported. Well nearly all of the UIKit
headers import Foundation.h
!
Hope this make sense.
Upvotes: 2
Reputation: 64002
If you take a look at some of the individual UIKit headers, they import Foundation themselves. As BoltClock noted, it's also in the standard .pch
The reason that it can be put in all these places is mentioned in "The Objective-C Programming Language":
This directive is identical to #include, except that it makes sure that the same file is never included more than once. It’s therefore preferred and is used in place of #include in code examples throughout Objective-C–based documentation.
You want to make sure that every file that needs access to the symbols defined in Foundation has them. The reason it's repeated is that it helps with reading and understanding the code; it says "This code relies on the stuff that's in this header".
Upvotes: 2
Reputation: 723598
It is imported in the precompiled header file (extension .pch
) in your project.
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
As to why UIKit.h
appears to have two import lines per file since it also shows up above, though, I'm not too sure. It shouldn't matter anyway, as the second #import
line won't cause UIKit to be included again if it's already included in the precompiled header file.
Upvotes: 8