If you import UIKit.h does that also automatically import Foundation.h?
UIKit.h doesn't explicitly include it, but I wouldn't be surprised if one of the other UIKit headers does.
However, all of your files will have it anyway, because your default pch (precompiled header, or the header that's automatically added to every file in your project) comes with this:
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
This means that every file in your iPhone app will automatically have Foundation and UIKit imported.
Related
I am confused by this. When I declare my view controller as .m file,
#import <AVFoundation/AVFoundation.h>
#import <Speech/Speech.h>
These two lines in ViewController.h won't cause linking issue. I didn't add any frameworks to my Xcode project.
But when I change it to .mm file because I need to use a C++ library, I got linking issue for the AVFoundation library, and I need to manually add it to my project General -> Link Frameworks and Libraries. Why? Why doesn't this issue appear when I am compiling an .m file?
I am getting a weird error on my application when trying to add an ObjC .h file to the bridging header. In particular, when I try to add the header for STXFeedPhotoCell.h, the project fails to compile with the following error (this error is displayed in STXFeedPhotoCell.h. Here is my bridging header:
#import "NetworkHelper.h"
#import "ArtistModel.h"
#import "UIImageView+Masking.h"
#import "CommentModel.h"
#import "LoginViewController.h"
#import "STXFeedPhotoCell.h"
And here is the error thrown:
/pathToProject/Helden der Volksmusik/STXDynamicTableView/Cells/STXFeedPhotoCell.h:12:9: 'NSDate+DateTools.h' file not found
Indeed, STXFeedPhotoCell.h, has an import statement for file NSDate+DateTools.h.
If I remove line #import "STXFeedPhotoCell.h" from the bridging header, then everything compiles fine.
Would appreciate any pointers on why the compiler is getting this error, or if there is a problem in the way these dependencies are being managed for swift (I am using cocoa pods with frameworks enabled).
To import DateTools and use it in Swift, you have to add this to your bridging file:
#import "DateTools/NSDate+DateTools.h"
Having a project that uses a Prefix.pch file to import some commonly used frameworks, for example:
// Prefix.pch
#import UIKit;
When a objective-c header that uses the included prefix frameworks is added to the bridging header compilation fails by not finding the classes imported by Prefix.pch:
// Bridging-Header.h
// UIKit will not be found and will cause errors
#import "FileThatUsesUIKit.h"
So far the cleanest solution found is to also include the Prefix.pch file before the rest of the includes, so that definitions in the prefix are available just like it is expected:
// Bridging-Header.h
#import "Prefix.pch"
#import "FileThatUsesUIKit.h"
Alternatively the specific framework can be imported into the Bridging-Header.h file directly, but that involves keeping on par the definitions between both the prefix file and the bridging header:
// Bridging-Header.h
#import UIKit;
#import "FileThatUsesUIKit.h"
Is importing the Prefix.pch file into the Bridging-Header.h file the correct approach? Are there any other alternatives?
I have a project setup using the UnitTest template provided by Apple. Too I added MagicalRecord to Prefix header. When I am running on the device and Simulator everything is working fine.
Except the Unit Tests, when I am compiling for the unit tests the build failed with the following command: 'CoreData+MagicalRecord.h' file not found . This happens in the prefix header.
prefix.pch
//
// Prefix header for all source files of the '123tv' target in the '123tv' project
//
#import <Availability.h>
#ifndef __IPHONE_3_0
#warning "This project uses features only available in iOS SDK 3.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Environments.h"
#import "CoreData+MagicalRecord.h"
#import "PBLog.h"
#endif
Has anyone an idea?
Make sure that the Header Search Paths is set up correctly for your test target.
I generally use CocoaPods which will automate this stuff for you
Try to run command (1) in terminal, then add import in step (2)
In your project directory
run pod update
You should now be able to add
#import <MagicalRecord/CoreData+MagicalRecord.h>
to any of your target's source files and begin using MagicalRecord!
While learning iPhone programming, every Xcode template I've seen includes an AppName-Prefix.pch file with the following contents:
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
My understanding is that this file's contents are prefixed to each of the source code files before compilation. Yet each of the other files also imports UIKit, which seems superfluous. For example, main.m begins...
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
...
Cocoa applications in Mac OS X do the same thing, importing Cocoa.h in both the prefix file and the header files.
Why have both? I removed the #import directives from all of the source files except the prefix file, and it compiled and ran correctly.
My understanding is that this file's contents are prefixed to each of the source code files before compilation
That's basically correct but you need to understand the subtle points: Every compilation from Xcode eventually boils down to an invocation of gcc or clang. What XCode does is to compile the X.pch file first:
clang -x X.pch -o X.pch.gch
and then when an individual source file (say a.m) is compiled, it issues
clang -include X.pch a.m -o a.o
which loads the pch file, triggering the use of the precompiled header. So, from the point of view of the compiler, it's not really that the content of the pch file is automatically prefixed. Rather, Xcode prefixes the precompiled header to a file when it invokes the compiler.
A future version of XCode might just stop doing it. So, it's better to keep #imports in your .m or .h files, too.
You can also think of it this way: the use of the pch file is just what Xcode does for us behind the scenes to speed up the compilation process. So, we shouldn't write codes in a way it essentially depends on it, like not importing UIKit.h file from our .m/.h files.
(Also, it seems to me that XCode4's syntax coloring gets confused if you don't import the appropriate header files correctly from the .h and .m files.)
Why have both?
Better safe than sorry. Precompiled headers can be disabled, and since #import won't import anything twice, the overhead is negligible.