I try to integrate Swift
code in my app.My app is written in Objective-C
and I added a Swift
class. I\'ve done everything described he
I have the same error: myProjectModule-Swift.h
file not found", but, in my case, real reason was in wrong deployment target:
"Swift
is unavailable on OS X earlier than 10.9; please set MACOSX_DEPLOYMENT_TARGET
to 10.9 or later (currently it is '10.7')"
so, when I've changed deployment target to 10.9 - project had been compiled successfully.
I spent about 4 hours trying to enable Swift
in my Xcode
Objective-C based project. My myproject-Swift.h
file was created successfully, but my Xcode
didn't see my Swift-classes
. So, I decided to create a new Xcode
Objc-based project and finally, I found the right answer! Hope this post will help someone :-)
*.swift
file (in Xcode) or add it by using Finder.Objective-C bridging header
when Xcode asks you about that.Implement your Swift class:
import Foundation
// use @objc or @objcMembers annotation if necessary
class Foo {
//..
}
Open Build Settings and check these parameters:
YES
Copy & Paste parameter name in a search bar
myproject
Make sure that your Product Module Name doesn't contain any special characters
YES
Once you've added
*.swift
file to the project this property will appear in Build Settings
myproject-Swift.h
This header is auto-generated by Xcode
$(SRCROOT)/myproject-Bridging-Header.h
Import Swift interface header in your *.m file.
#import "myproject-Swift.h"
Don't pay attention to errors and warnings.
I had the same problem and finally it appeared that they weren't attached to the same targets. The ObjC class is attached to Target1 and Target2, the Swift class is only attached to the Target1 and is not visible inside the ObjC class.
Hope this helps someone.
Also probably helpful for those of you with a Framework target:
The import statement of the auto-generated header file looks a bit different from app targets. In addition to the other things mentioned in other answers use
#import <ProductName/ProductModuleName-Swift.h>
instead of
#import "ProductModuleName-Swift.h"
as per Apples documentation on Mix & Match for framework targets.
I just discovered that adding a directory of swift files to a project won't work. You need to create a group first for the directory, then add the swift files...
Make sure your project defines a module and you have given a name to the module. Then rebuild, and Xcode will create the -Swift.h
header file and you will be able to import.
You can set module definition and module name in your project settings.