I am trying to reference some Swift-defined classes from my Objective-C implementation file, but for some reason, though I've gotten the header file to auto-generate, it doesn't appear to be including any information about the Swift classes in the project.
My Swift class is attributed with #objc yet even after importing the "-Swift.h" file, I still get a "Use of undeclared identifier" error when compiling.
I can't figure out what I'm missing. I have Defines Modules set to YES in the project.
Also of note: if I command-click the symbol from my Obj-C file, Xcode successfully finds the definition in the Swift file.
Make sure in build setting you have got this setup:
Objective-C Bridging Header : $(SRCROOT)/Sources/SwiftBridging.h
Sometime when you import a swift file directly Xcode don't prompt you to add a bridging header. it's a must have step even you don't call objective-c from swift.
You may need to derive from NSObject.
I encountered a similar situation where some Swift classes were not being exposed to Obj-C through the auto generated header. The solution is to derive Swift classes from NSObject.
class SwiftClassNotInHeader { }
class SwiftClassInHeader : NSObject { }
From MyApp-Swift.h
SWIFT_CLASS("_TtC6Server18SwiftClassInHeader")
#interface SwiftClassInHeader : NSObject
- (SWIFT_NULLABILITY(nonnull) instancetype)init OBJC_DESIGNATED_INITIALIZER;
#end
SwiftClassNotInHeader is not in myApp-Swift.h
Related
This question is basically the same as this one, but that one went unanswered.
TL;DR: Protocol does not show up in MyProject-Swift.h secret bridging file. And so (not unexpectedly) the Obj-C code can't find it.
I have been able to duplicate it in a from-scratch project. Here is what I did.
Create new one-view iOS Objective-C project.
Add a new Swift UIView subclass via "New File" menu.
Confirm that MyProject-Bridging-Header.h was created. (Visibly)
Confirm that the secret MyProject-Swift.h file was automatically created. (Way down there in DerivedData/.../DerivedSources).
Add an instance of this View to my ViewController.m.
Include the MyProject-Swift.h file in ViewController.m.
Successfully build -- sanity check
Now for the weirdness...
Add a Swift protocol file via "New File" menu.
Make ViewController conform to and implement this protocol.
Build fails at this spot in ViewController.m:
#import "MyProject-Swift.h"
#interface ViewController () <MyProtocol> // Can't find protocol declaration
Examining the MyProject-Swift.h file confirms that the protocol is not in there. But poking around some big pre-existing hybrid projects (that do build) I don't see any protocols in any of their -Swift.h files.
How do Swift protocols get recognized in Objective-C?
I have done this for my project
while defining protocol you can add #objc before the protocol definition
#objc protocol NewViewControllerDelegate {
}
And you are done
Note : I think #objc makes swift protocols and classes visible to objective c
My Project is in Swift 3.0 and I am using xCode8.2. I need to import objective C code in my swift project. objective C required app delegate reference. How can I get swift appdelegate reference from my objective C code?
In my objective C file #import "AppDelegate.h" is giving not found error as it is swift AppDelegate
As Kie already said, you need to #import "<ProductModuleName>-Swift.h" in your Obj-C .m file to get all Swift classes. Here a sample project with what you need https://github.com/aminbenarieb/Researches/tree/master/Swift/Swift_ObjC.
Update:
If your can't see Swift files in Objective-C files, make sure you have the Objective-C bridging header, as follows in Apple Documentation:
Importing Objective-C into Swift
To import a set of Objective-C files in the same app target as your
Swift code, you rely on an Objective-C bridging header to expose those
files to Swift. Xcode offers to create this header file when you add a
Swift file to an existing Objective-C app, or an Objective-C file to
an existing Swift app.
I am not sure if this answers your question.
Getting AppDelegate in Swift
You can call objective-c code from Swift and if your Objective-C code needs an AppDelegate for it to work in some calls you can gat this by calling UIApplication.shared.delegate in Swift.
let appDelegate = UIApplication.shared.delegate as! AppDelegate
Getting AppDelegate in Objective-C
If you need the AppDelegate in your Objective-C code thats the way:
#import "<ProductModuleName>-Swift.h" // You have to replace with your swift module name
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
Adding some color to the answers from Kie and Amin: I was struggling with how to adapt #import "<ProductModuleName>-Swift.h" into something that made sense for my project. I was getting header file not found compile-time error, and it turned out that non-alphanumeric characters in ProductModuleName are replaced with underscores when the bridge header is created. In my case I had a space in the product name; once I replaced the space with an underscore the compile-time error was resolved. Seems trivial now, but I'm just starting out.
Here's the reference to Apple's documentation and relevant snippet:
The header's name is generated from your product module name, followed by "-Swift.h". By default, this name is the same as your product name, with any non-alphanumeric characters replaced with an underscore (_). If the name begins with a number, the first digit is replaced with an underscore.
Importing Swift into Objective-C
When you add a Swift file to an Objective-C project, Xcode will generate a Swift-to-ObjC header, as described here: http://ericasadun.com/2014/08/21/swift-calling-swift-functions-from-objective-c/
Without this header it is not possible to call Swift code from Objc-C. However Xcode is not auto-generating this header for my framework target.
If I create an Objective-C app and drop a Swift file into it, then it does auto-generate one, so I suspect it's because I'm building a framework and not an app. Without one its not possible to use the Swift code from the Obj-C code.
I tried using the one which was generated for the app (after renaming it and putting it in the appropriate DerivedData folder ) but Xcode didn't update it and actually it will eventually delete it, so manually creating or trying to maintain this file is not feasible.
How can I make Xcode generate this header for a framework target, so that I can call my Swift code from my Obj-C code?
And remember folks: the question is about calling Swift from Obj-C not calling Obj-C from Swift.
I created a new Framework project, added both Obj-C and Swift files, and was able to do this:
// MyObjCClass.m
#import "MyObjCClass.h"
#import <MyFramework/MyFramework-Swift.h>
#implementation MyObjCClass
- (void)test {
[[MySwiftClass alloc] init];
}
#end
Note that your Swift class must be public:
public class MySwiftClass: NSObject {
// ...
}
More information is available in Apple's Swift/Obj-C interop documentation under "Importing Swift into Objective-C".
I have been reading through Apple documentation and as I read it is possible to use Swift classes in existing Objective-c project.
My question is, Can I use Swift code with objective-c code in the same class, in my existing Objective-c project?
Thanks
If you mean Objective C and Swift in the same file, no, you can't.
What you can do is create a class in Objective C, and add extensions in a Swift file, or the opposite
No, a class must be defined in Swift or in Objective-C not mixed language inside single class file, sorry .
As you may have already noticed a class in swift is generally made by a single file with .swift extension, while objective-C class is defined using two files .h and .m . So, a .h or .m can't contain swift code, the compiler will gives you some errors ...
I am integrating Swift into a large existing Objective C project and have run into what I think is a circular reference.
The classes in question are as follows:
Objective C Controller
#import "Hopscotch-Swift.h"
#interface MyController : UIViewController<MyProtocol>
...
#end
Swift Protocol
#objc protocol MyProtocol: NSObjectProtocol {
...
}
Bridging Header
#import "MyController.h"
This code fails to compile because the Hopscotch-Swift.h file will not generate.
I think this is due to a circular reference error as I can import Hopscotch-Swift.h into objective c headers that are not included in Hopscotch-Bridging-Header.h and it works fine.
Is there a workaround for this issue or should I file a radar with Apple?
Forward declaration should work, in your case.
In your .h:
#protocol MyProtocol;
#interface MyController : UIViewController<MyProtocol>
#end
In your .m:
#import "HopScotch-Swift.h"
From How can I add forward class references used in the -Swift.h header? and the Swift interoperability guide:
If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types prior to importing the Swift generated header into the Objective-C .m file you want to access the Swift code from.
I ran into this when trying to use Swift classes inside Objective-C protocols, where the protocol was also implemented by another Swift class. It reeked of circular references and I guessed that it might be a problem trying to circularly generate the bridging headers, rather than a 'normal' circular include problem.
The solution, for me, was to just use forward declarations before the protocol declaration:-
// don't include the MyProject-Swift.h header
// forward declaration of Swift classes used
#class SwiftClass;
#protocol MyProtocol <NSObject>
- (SwiftClass *)swiftClass;
#end
The forward declaration by itself didn't work for me. It compiled without errors but still had warnings that the protocol couldn't be found. I treat all warnings as errors, so this isn't good enough.
I was able to fix it by moving the protocol implementation into another category header.
So here's what worked for me:
In my MyOtherSwiftFile.swift:
#objc protocol MyProtocol: class {
func viewController(didFinishEditing viewController: MyViewController)
}
In my MyViewController.h:
#interface MyViewController // Removed protocol implementation declaration here
#end
Added MyViewController+MyProtocol.h to project, and put this in there:
#interface MyViewController (MyProtocol) <MyProtocol>
#end
The methods themselves can stay where they are if you want.
After you implement the above and compile, you'll get compiler warning(s) somewhere in your code that requires that MyViewController implements MyProtocol. In that file, you will #import "MyViewController+MyProtocol.h"
Alternatively you can convert your protocol to an Objective-C protocol MyProtocol.h and then use it in Swift by including MyProtocol.h in your bridging header.
You could something like this in the .h file you suspect to trigger the circular reference:
#ifndef MY_HEADER_H
#define MY_HEADER_H
your header file
#endif