How to see the Objective-C names for Swift functions - objective-c

Is there a way for me to see the Objective-C translated names for all Swift functions in a file in Xcode?

Objective-C code imports Swift by including an auto-generated
header file which contains the Objective-C interface of all (Objective-C
visible) Swift classes and methods:
#import "<ProductModuleName>-Swift.h"
To view the contents of this file:
Command-click on "<ProductModuleName>-Swift.h" in the Xcode
source file editor,
choose "Jump to Definition".

Related

view swift interface for Objc file

I am working in a mixed ObjC Swift project. I am currently overriding functions that where written in ObjC in a swift subclass. I'm wondering if there is a quick way to view the auto generated Swift header files for the objC file so that I can check the syntax of functions after they have been converted to swift.
I'm wondering if there is a quick way to view the auto generated Swift header files for the objC
Switch to the Generated Interface in the first menu of the jump bar.
Example: before:
after:

How I can get reference of Swift AppDelegate from objective c code

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

How do I call Swift code from Objective-C in a Framework target?

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".

Auto-generated "ModuleName-Swift.h" header not including Swift classes

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

Can we use swift with objective-c in same class

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 ...