ObjectiveC target can't find module, Swift working fine - objective-c

I'm building a framework that needs to work with Swift and Objective-C. It is built with Swift.
It works fine in a Swift app. #import MyFrameworkName works just as expected. But I can't seem to make it work in an Objective-C app.
#import <MyFrameworkName/MyFrameworkName-Swift.h>
This is the build log:
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "Headers/MyFrameworkName.h"
^
/Users/user/Projects/MyFrameworkName/Sources/MyFrameworkName.h:20:9: error: module 'CommonCryptoBridge' not found
#import CommonCryptoBridge;
^
<unknown>:0: error: could not build Objective-C module 'MyFrameworkName'
This is my framework header:
#import <UIKit/UIKit.h>
FOUNDATION_EXPORT double MyFrameworkNameVersionNumber;
FOUNDATION_EXPORT const unsigned char MyFrameworkNameVersionString[];
#import CommonCryptoBridge;
The CommonCryptoBridge is a module to enable CommonCrypto in Swift, defined by CommonCryptoBridge.h and CommonCryptoBridge.modulemap:
CommonCryptoBridge.h
#ifndef __COMMONCRYPTO_BRIDGE__
#include <CommonCrypto/CommonCrypto.h>
#define __COMMONCRYPTO_BRIDGE__
#endif /* __COMMONCRYPTO_BRIDGE__ */
CommonCryptoBridge.modulemap
module CommonCryptoBridge [system] {
header "CommonCryptoBridge.h"
export *
}

Related

Header file not found in Bridging Header

I've looked through a number posts and cannot find a solution to this problem.
I installed the MMLanScan Objective C library into in my pod file successfully.
Then I created a bridging header file in my project directory
#ifndef BridingHeader_h
#define BridingHeader_h
#import "MMLANScanner.h"
#import "LANProperties.h"
#import "PingOperation.h"
#import "MMLANScanner.h"
#import "MACOperation.h"
#import "MacFinder.h"
#import "MMDevice.h"
#endif
I also set the header file path in my project's compiler settings
But when I build my app, I get two compile time errors
error 1:
MMLANScanner.h file not found
error 2:
Failed to emit precompiled header `/Users/my user name/Library/Developer/Xcode/Derived Data/My Project Name...
Both these errors disappear when I delete my imports from the bridging header file.
Any clues how to compile this library would be appreciated.
edit
So the required .h files appear to be in my pod directory, so not sure why I get these errors
So the solution was as simple as adding the relative folder path to the header file imports
#ifndef BridingHeader_h
#define BridingHeader_h
#import "MMLanScan/MMLANScanner.h"
#import "MMLanScan/LANProperties.h"
#import "MMLanScan/PingOperation.h"
#import "MMLanScan/MMLANScanner.h"
#import "MMLanScan/MACOperation.h"
#import "MMLanScan/MacFinder.h"
#import "MMLanScan/MMDevice.h"
#endif
not
#ifndef BridingHeader_h
#define BridingHeader_h
#import "MMLANScanner.h"
#import "LANProperties.h"
#import "PingOperation.h"
#import "MMLANScanner.h"
#import "MACOperation.h"
#import "MacFinder.h"
#import "MMDevice.h"
#endif

Swift Bridging Header - Use of undeclared type 'MMLANScannerDelegate'

I successfully installed this Objective C library MMLanScan, successfully created a bridging header file, and successfully built my project without compile time errors.
But now when I extend from MMLANScannerDelegate I get a compile-time error, for example compiling this
class MyViewController: UIViewController, MMLANScannerDelegate {
// ...
}
results in this error
Use of undeclared type 'MMLANScannerDelegate'
Here is my bridging header file which I successfully compiled, so I'm not sure what the problem is
#ifndef BridingHeader_h
#define BridingHeader_h
#import "MMLanScan/MMLANScanner.h"
#import "MMLanScan/LANProperties.h"
#import "MMLanScan/PingOperation.h"
#import "MMLanScan/MMLANScanner.h"
#import "MMLanScan/MACOperation.h"
#import "MMLanScan/MacFinder.h"
#import "MMLanScan/MMDevice.h"
#endif

NSStringFromClass() is invoked by UIApplicationMain(), but Foundation.h is not imported in main.m

(a) The 4th parameter in the UIApplicationMain() method is the method:
NSStringFromClass()
(b) NSStringFromClass() is found in Foundation.h
(c) The only import into main.m is UIKit.h.
(d) As best I can tell, UIKit.h does not extend the Foundation.h
So, why am I not getting an error when I compile main.m? NSStringFromClass() should be an unrecognized method?
Have a look at the macro in the *prefix.pch file in your project supporting files ;-)
Foundation is imported in the precompiled header that comes standard with every Xcode project:
//
// Prefix header for all source files of the <x> target in the <x> 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>
#endif

Why does syntax highlighting for Foundation Objects turn off when not importing Foundation Framework

My *-Prefix.pch file looks like the following
//
// Prefix header for all source files of the 'stuff' target in the 'stuff' project
//
#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>
#endif
So why is it then when I remove #import from a file in Xcode 4.6.2 all the syntax highlighting turns off for all Foundation Framework objects? The code still compiles and run correctly.

How to #import <NSObjCRuntime.h> to use objc_msgSend

I'd like to import runtime's header to use objc_msgSend but I'm getting:
error: NSObjCRuntime.h: No such file or directory
Should I add something to the header search path?
You need to include <objc/message.h> (you'll find the related headers in /usr/include/objc) and link to the objc (/usr/lib/libobjc.dylib) library.
#import <Foundation/NSObjCRuntime.h> does work
but you probably need
#import <objc/runtime.h>
like this Apple example does
upd: since iOS 7 #import <Foundation/NSObjCRuntime.h> replaced to #import <objc/NSObjCRuntime.h> but i recommend to use #import <objc/runtime.h> anyway
When using Xcode 6 and later, you will get an error after #include<objc/message.h>. It can be resolved like this
#include <objc/message.h>
void foo(void *object) {
typedef void (*send_type)(id, SEL, int);
send_type func = (send_type)objc_msgSend;
func(object, sel_getUid("foo:"), 5);
}
http://devstreaming.apple.com/videos/wwdc/2014/417xx2zsyyp8zcs/417/417_whats_new_in_llvm.pdf