Can we use swift with objective-c in same class - 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 ...

Related

How to see the Objective-C names for Swift functions

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

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 to hide an Objective C declaration from Swift

I'm working on a library written in Objective-C. I want to have some methods available to callers written in Swift, and not available to callers written in Objective-C.
Going in the other direction, you can control Swift visibility in Objective-C with the #objc and #nonobjc attributes. Are there equivalents to make some Objective-C methods Swift-only or Objective-C-only?
Would I have to maintain a separate header? And if so, is there any way I could require Swift users to import Library-Swift.h in their bridging headers instead of Library.h?
You need separate headers,one for swift and one for objc(for the class).And for swift you could make a header for the whole framework(just for swift,and where you put the swift header for that class),and who wants to use your framework in swift include that header into their bridging header.

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