How I can get reference of Swift AppDelegate from objective c code - 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

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

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

XCode Throwing Errors #implementation

I'm trying to learn some of the basics of developing OS X apps with XCode and Objective-C, but I am already running into problems.
I have a project I made from a while back which worked very well for me, however, when I try to replicate the results I had last time, I run into numerous errors.
I have two files, a .c and a .h named "AppDelegate"
in AppDelegate.h:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebView.h>
#interface AppDelegate : NSObject {
IBOutlet WebView *gameFrame;
}
#end
then, in AppDelegate.c:
#import "AppDelegate.h"
#implementation AppDelegate
-(void)awakeFromNib
{
}
#end
In IB, there is an NSObject named 'AppDelegate' and its class is 'AppDelegate'.
However, when I try to run this, I get 11734 errors...
When I click on the error icon at the bottom of the XCode window, it lists a bunch of code that seems to be involving NSStrings, but I cant make any sense of it...
Also, within my code, the
#end
line in both the .c and the .h are highlighted with an error saying:
'Expected identifier or '(' before '#' token.'
I don't understand what XCode is tripping up on when it tries to compile, I don't see any logical place for a '(' to go and I don't think I left anything unidentified.
Any help would be appreciated.
That's because that isn't valid C code.
You named your module file AppDelegate.c, which indicates that it contains source code written in (more or less) pure C. But it does not: You wrote a class interface and implementation in Objective-C, which is a superset of C (all C is valid Objective-C, but not all Objective-C is valid C—in particular, classes aren't).
For this, you must name the module file AppDelegate.m (or anything else, as long as it ends with .m; naming it after the class is a convention worth following). The .m suffix indicates a module (usually containing a class implementation) written in Objective-C.
So, just rename your module file from AppDelegate.c to AppDelegate.m. Make sure you do this in Xcode, not the Finder: If you do it in the Finder, Xcode will only care that there is no longer a file named AppDelegate.c; it won't notice the rename.
For your convenience in creating future classes, Xcode provides a template in the “New File” panel for creating subclasses of certain Cocoa classes; your AppDelegate should be a subclass of NSObject, and templates are also provided for NSView, NSDocument, UIView, UIViewController, and a few others. The files created by the template will already have the correct extensions.