I have a framework (let's call it MyKit) written in Objective-C that I'm extending with some Swift classes. I'm trying to get my head around it using this documentation: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_77
As far as I understand, I'm not supposed to have a bridging header class, and instead put all my includes in the umbrella header file (that I understand to be ).
So I write in MyKit.h:
#import <MyKit/ModelObjectA.h>
#import <MyKit/ModelObjectB.h>
#import <MyKit/ControllerObjectC.h>
I don't list ControllerObjectD.swift, even though it goes into here as well? Or should I include
#import <MyKit/ControllerObjectD-Swift.h>
?
ControllerObjectD uses ModelObjectA and ModelObjectB. Now that I don't have a bridge headerfile, I get compile errors in it because it cannot find these objects.
The documentation says "Swift will see every header you expose publicly in your umbrella header." and this is true when I import the framework into other projects, but the framework project cannot compile because it doesn't see it. I have turned on the "Define Modules" build setting.
Is there something I've misunderstood about the umbrella header, perhaps? Where can I say "hi project, this is the umbrella header file"? The framework compiles if I set the umbrella header file as bridging header, but that sounds like I've come back to the beginning this way?
Cheers
Nik
I believe your problem may be down to the Access Modifiers in your Swift class, however I've written a short guide and sample project to help you:
Sample project can be found here
There are two parts to having a mixed language framework:
Importing Objective-C into Swift
Importing Swift into Objective-C
1. Importing Objective-C into Swift
This is, for example, if you have an Objective-C class named Player that you want to add to a swift class called Game.
According to the documentation, you need to do these two steps to import the Player object into the Game object.
Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.
In your umbrella header file, import every Objective-C header you want to expose to Swift.
#import <Framework/Player.h>
Make sure your Player header file in Objective-C is marked for public target membership in the framework:
Following these steps, it's possible to import the Player Objective-C class into the Game Swift Class:
import UIKit
public class Game: NSObject {
public let player: Player
public init(player: Player) {
self.player = player
super.init();
}
}
2. Importing Swift into Objective-C
For importing the Swift Game class into the Player object, we can follow a similar procedure.
As before; Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.
Import the Swift code from that framework target into any Objective-C .m file within that framework target using this syntax and substituting the appropriate names:
#import <ProductName/ProductModuleName-Swift.h>
In my case, this works as:
#import <SwiftObjC/SwiftObjC-Swift.h>
and I assume, for you:
#import <MyKit/MyKit-Swift.h>
Therefore, make sure that all the properties, methods and classes you want to access are defined as public in your swift file or else they won't be visible to Objective-C .
I have uploaded my sample project showing how this all works on GitHub https://github.com/elliott-minns/SwiftObjCTestFramework
I hope this helps you with your problem.
I think you need to do bridging-header to access your obj-c code in swift.
More in the link
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
so it is a file with your imports from obj-c transfering to swift. names are very important so care about that. hope it helps
p.s. also you need to add this header path to your project settings
From the Documentation at Apple: A Swift class must be a descendant of an Objective-C class to be accessible and usable in Objective-C. For more information about what you can access from Objective-C and how the Swift interface is imported, see Swift Type Compatibility.
So your public swift class needs to extend NSObject for example.
Related
Cannot get Xcode to generate the bridging file so it's currently empty and cannot find any guide/docs on what needs to be in the file. The empty bridging file is being imported in the .m where the class is needed. Swift file using #objcmembers on class definition.
How and what should go into the bridging file. Have looked for two days trying to make this work with no success.
Is there a way to turn back on the automatic generation of the bridging file in Xcode?
Any docs on the contents of the bridging file?
Don't confuse the two kinds of header. The "bridging header" is for Swift to see Objective-C code. It isn't relevant here.
The way Objective-C sees Swift code is through the generated interface header. You can see its name in your app target build settings:
So what that tells you is that if you #import "MomApp2-Swift.h" in your .m file, your Objective-C code will see your code that is exposed from Swift.
If you want to see what is in that generated interface header, then after you've done that import, Command-click on the file name.
To use a Swift class on objective-c you need to be sure that your class inherits from NSObject, has an #objc annotation on class definition and other elements that you want to use need to be marked with #objc. #objcmembers marks all elements visible from an Objc class located on file that imports the "project-Swift.h"
You may not need all class members visible from Objective-c
In an iOS application I have a subproject (not cocoapods) in which I have included a swift file and ObjC file (that is used by the swift file). XCode automatically created a bridging file but could not build it because apparantly bridging is not allowed in a framework. The workaround that I used was to add the objective-c header to the umbrella file and it worked. Now I need to use a swift class from ObjC. I have define module to set to YES, the generated file Framework-Swift.h . But when I try to import it in objective-c i get
Could not build Objective-C module
The closest I got after some googleing was this answer:
Ah gotcha. It looks like you're building a mixed Swift & Objective-C
pod - if that's the case, Xcode will try to import
within the generated -Swift.h header.
You'll need to create the header manually and add imports for the
Objective-C classes that you want to expose to Swift.
CocoaPods generates an umbrella header automatically and imports it
within the .modulemap, but Xcode doesn't use that when generating the
-Swift.h header
But I am unsure what header needs to be created manually.
Any ideeas or pointer about using swift in an objective-c framework ? In both ways ?
I also had similar issue when using Swift pods in my Swift project containing several targets. No Objective-C code at all. I tried to clear build folder, pods cache, derived data - nothing worked.
Solution:
Open the Build Settings for a target that contains your module code. Set the "Install Objective-C Compatibility Header" to "No"
There's a great and simple article that wraps up this case:
DEFINES_MODULE=YES
To use ObjC classes in Swift, create a bridging header and specify path to it in the build settings
To use Swift classes in ObjC, #import <ModuleName/ModuleName-Swift.h> in your *.m file (use #class SwiftClass; forward declaration in *.h file, if needed)
For swift classes and their members to be visible to objc
inherit your class from NSObject
prepend it with #objcMembers
make both the class and its members public
I have a project with large amount of obj-c and swift code inside it.
I have an obj-c class which imports ProjectName-Swift.h to let that class access swift code.
Now I need to expose that obj-c class to the swift so I can access it from swift code.
The problem is that after import in bridging header name of obj-c class project stops building with error that it cannot find ProjectName-Swift.h which import is stated in obj-c class.
I cannot remove import of ProjectName-Swift.h from obj-c class because after that class breaks.
What can I do ?
OK, had one answer and then re-read the question. Make absolutely certain that your import of the swift header is the Module Name of the project (not necessarily the project name):
Apple Documentation: Swift and Objective-C in the Same Project
More specifically, go to the section marked "Importing Swift into Objective-C" and read it. And even more specifically:
Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to “Yes”.
Import the Swift code from that framework target into any Objective-C .m file within that framework target using this syntax and substituting the appropriate names:
#import <ProductName/ProductModuleName-Swift.h>
Trying to understand how I can use this in my swift project.
https://github.com/Grouper/FlatUIKit
I have copied the classes folder into my project but am not sure how to use the various .h and .m files. How would I go about using these files within my storyboard to use the custom appearances?
Did some searching and wasn't really able to understand the various threads that I found.
A bit of a broad question so I'll try to provide some basics and hopefully that helps.
If you are unaware, there is a whole book written by Apple about how to use objective-c with swift. it's available on iBooks for free Using Swift with Cocoa and Objective-C (Swift 2.1) by Apple Inc
Here is the online link
You will need to create an objective-c bridging header and import your code through the header.
Your bridging header would look like this:
#import "XYZCustomCell.h"
#import "XYZCustomView.h"
#import "XYZCustomViewController.h"
If they don't have any modules then you can use them in your swift code and it should see them. According to Apple:
Use your custom Objective-C code with the same Swift syntax you use with system classes.
let myCell = XYZCustomCell()
myCell.subtitle = "A custom cell"
If you are importing an Objective-c framework then it should already contain and umbrella header file that takes care of the bridging header stuff for you. then you just import the framework name into the class that you are planning on using.
import MyCustomFramework
Link to the apple docs here
For this, you can use a bridging header, which is used to expose Objective C code to swift. To do this, make a new header file in your project. Import the header files you would like to use in the new header file like this:
#import <FlatUIKit/FlatUIKit.h>
And then, in the build settings of your project, define the Objective-C bridging header to be the header file you just created. Now in any swift files that you would like to use the library in just import the classes like this:
import FlatUIKit
Hope this helps!
I have a mixed Swift and Objective-C project. To access a Swift public API in Objective-C, the API consumer currently has to import "MyModule-Swift.h". I already have a Objective-C umbrella header called "MyModule.h". But, importing "MyModule.h" is not going to work for the Swift APIs. I tried importing "MyModule-Swift.h" in the umbrella header, but it doesn't find it (I'm assuming since it is generated on the fly by Xcode).
Any suggestions so that an API consumer can always import "MyModule.h" to use public APIs written in Swift/Objective-C will be really appreciated.
====================================================================
Edit: I apologize for not taking the time to properly frame the question. I have a framework called MyModule.
I have a Objective-C class called ABUser,
#interface ABUser: NSObject
- (void)walk;
#end
I wanted to add new behavior to this class using Swift, so I defined an extension
extension ABUser {
func swiftWalk()
}
Now, say I wanted to access the swiftWalk method defined on ABUser from an Objective-C app, I would have to #import <MyModule/MyModule-Swift.h>. #import <MyModule/MyModule.h> would work if I wanted to use the walk method. This is assuming the umbrella header MyModule.h imports ABUser.h.
I always wanted the Objective-C app to #import <MyModule/MyModule.h> and never have to worry about whether an API was written in Objective-C or Swift in the framework. So, I tried importing MyModule-Swift.h in the umbrella header MyModule.h. But, my Objective-C app didn't compile if do that. Not sure, if this is because MyModule-Swift.h is generated by Xcode on the fly during the build process.
Edit 2: Added a sample project to reproduce this issue here: https://github.com/priteshshah1983/MyObjectiveCApp
The relevant code is in ViewController.m. The build will fail with the master branch. To get it work, checkout the working branch.
The reason that using #import MyModule; worked, is that "modules are a packaging together of the framework executable and it's headers" (from this SO post).
In other words, when you #import MyModule;, Objective-C automatically imports all the swift and Objective-C headers related to the module, while you cannot include the swift header from the umbrella header. I suggest you take a look at the differences between #import and #import in the linked SO answer.
It turns out that using #import MyModule; instead of #import <MyModule/Module.h> allows using walk and swiftWalk methods as expected from the Objective-C app.
I honestly don't understand the details, but I hope this helps someone else. Please feel free to explain!