I have an Objective-C category that I've been using for a while for both iOS and OSX projects and I want to use it in a Swift project as-is...until I have the time to translate it to Swift.
Here's the top of my category's .h file:
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#else
#import <Cocoa/Cocoa.h>
#endif
I've included it in the bridging header, but when I build the project for iOS, I get an error saying that it can't find the file Cocoa/Cocoa.h.
Why is it even looking for it? Doesn't the conditional compile still work even in a Swift project? It's still compiling an Objective-C file.
Thanks.
Add #import "TargetConditionals.h" in your source file.
#import "TargetConditionals.h"
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#else
#import <Cocoa/Cocoa.h>
#endif
Or add "-DTARGET_OS_IPHONE" to the "Other C Flags" section of the target build options.
Related
QUESTION: What and where is the pre-compiled header file in an xcode project?
PROBLEM: I need help with xcode.
DISCLAIMER: I know nothing about xcode.
BACKSTORY: I have an app built with appcelerator, which builds an xcode project for me. Now I want to make it Citrix Ready which by this press release should be easy http://www.appcelerator.com/press-releases/citrix/ but < rant > they have no documentation on this, no one at appcelerator seems to care, and they say to use SO for support (which I did and it got me the peer pressure badge, yay!).< /rant >
So, since they suck I'm trying to do it myself directly in xcode and with a question that is acceptable within SO guidelines.
SITUATION: I'm up to step 3 of the steps to do this are listed on the Citrix website: http://docs.citrix.com/en-us/mdx-toolkit/10/xmob-mdx-dev-guide-overview/xmob-mdx-dev-ios-apps.html#par_anchortitle_33e5
Step 3: Revise a line of code in the pre-compiled header file in the app project to import WorxEnable.h from Worx.framework as shown in the following example
#ifdef__OBJC__
_
//import MDX extensions
#import <AVFoundation/AVFoundation.h>
#import <SystemConfiguration/SCNetworkReachability.h>
#import <Worx/WorxEnable.h>
#endif
I just don't know what or where the pre-compiled header file is.
Any help would be much appreciated.
I look forward to posting questions about steps 4 and 5 asking what those mean and how to do them:
Step 4: Add the following to “other linker flags” if they do not already appear:
Step 5: Add the following frameworks and libraries:
Thanks!
Hum, I think it's the .pch file.
If it not exist in your Xcode project (Xcode does not create automatically this file now), create one like myproject-Prefix.pch (just an example).
And in the build settings, add your file path in Prefix Header
My pch file :
#import <Availability.h>
#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <SystemConfiguration/SystemConfiguration.h>
#endif
I have a problem
#import Foundation;
and I see:
#import vs #import - iOS 7
and I set "Enable Modules" to "YES"
and my problem is not solved
I got this warning in a zero-swift project whenever I tried to add the #import SafariServices; statement.
Solution:
Enable the modules. Go to the Target > Build Settings and set the Enable Modules (C and Objective-C modules) to YES.
OR
Note: I haven't verified this potential solution, but probably worthy of consideration if there are side effects caused by this solution.
Rather than enabling modules to entire project, we can enable modules for a specific file which is importing c++ file. Go to build phases -> Compile Sources -> Select the file -> Add compiler flag -fmodules
The possible cause is that you use Objective-C++. Then modules get disabled despite the proper build settings.
I've been mixing ObjC, ObjC++, C++, and Metal. Whenever I get the "use of #import when modules are disabled" I try
replacing:
#import Name;
with:
#import "Name/Name.h"
example, replace:
#import Metal;
#import MetalKit;
#import CoreVideo;
with:
#import "Metal/Metal.h"
#import "MetalKit/MetalKit.h"
#import "CoreVideo/CoreVideo.h"
It seems to work.
Check if you are using #import "ProductName-Swift.h" somewhere in .mm files or any other files other than objc files.
Because if you use this import in cpp files then modules gets disabled automatically.
This check is in my AFHTTPClient.m file. The check is actually used very frequently throughout the file and the .h file. In one case, it does this:
#ifdef _SYSTEMCONFIGURATION_H
self.networkReachabilityStatus = AFNetworkReachabilityStatusUnknown;
[self startMonitoringNetworkReachability];
#endif
This is still version 1.X of AFNetworking.
Thanks!
I believe this is a pre-processor statement checking that you have linked your project with the SystemConfiguration framework. If not, I believe that AFNetworking was omitting some feature, or maybe it was giving you a nice warning to make sure you don't forget to include this framework, so that AFNetworking could work as expected.
Update
#ifdef _SYSTEMCONFIGURATION_H
#import <netinet/in.h>
#import <netinet6/in6.h>
#import <arpa/inet.h>
#import <ifaddrs.h>
#import <netdb.h>
#endif
So what's going on is if _SYSTEMCONFIGURATION_H is defined (internally by the SystemConfiguration framework), then AFNetworking knows that you included this framework in your project, and so it is importing a bunch of things to make use of it. Otherwise it isn't. This means that you don't have to include this framework if you don't want to.
To solve this problem, type
#import <SystemConfiguration/SystemConfiguration.h>
#import <MobileCoreServices/MobileCoreServices.h>
before importing AFNetworking.
I'm currently trying to compile my project just after adding some C code.
I'm using the Paul Kocher's blowfish algorithm implementation available on Bruce Schneier's website.
Since I included blowfish.c & blowfish.h in my workspace, my compiler is running crazy. Like if it did not recognize Objective-c code, pointing errors on NSObject class!
I tried to .mm the calling class but the problem stays.
Each answer found on SO talks about including C++ file, but it's not my pb...
Maybe a compiler directive that i'v missed ?
Most likely, what is happening is that the compilation of blowfish.c is using your previously established precompiled header (.pch) file, and that is including an Objective-C framework. Just disable the precompiled header and you should be OK. You might be able to conditionalize those frameworks, but personally, I find precompiled headers more trouble than they’re worth.
Thanks to microtherion, I found the problem.
My .pch file was declared as :
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
#import "AppDelegate.h"
#import "UINavigationController+Rotation.h"
#import "Categories.h"
The 3 last #import'ed files are objective-C.
I've just changed the #endif place to:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "AppDelegate.h"
#import "UINavigationController+Rotation.h"
#import "Categories.h"
#endif
I have defined many constants in GlobalVar.h and other .h files. I import these files in the Prefix.pch file like this :
//
// Prefix header for all source files of the 'XXX' target in the 'XXX' project
//
#import "GlobalVar.h"
[...]
#ifndef __IPHONE_3_0
#warning "This project uses features only available in iPhone SDK 3.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#endif
But when I compile the project all the #defined identifiers are missing and reported as "Use of undeclared identifier XXX".
I searched in the Build settings and the PCH file is set as "Prefix Header"... I am on Base SDK 4.3 and XCode 4.0.2
Do you have hints to debug this ?
Thanks for your help
I came across this error yet, after cleaning DerivedData and restart Xcode I fix it. Hope to help.
move your import to like so
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "GlobalVar.h"
#endif