Calling objective c function from SWIFT through bridging header - ios

I have ported an objective-c package into a swift package and included it within the bridging header. Everything works fine, except there is a function I have to implement from a delegate I can't get working:
- (void) mdwamp:(MDWamp*)wamp sessionEstablished:(NSDictionary*)info;
Does anyone know how I can implement this in SWIFT?

In the bridging header make sure you imported the objective-c header.
After doing that, you can use the following.
var instance: ClassName = ClassName()
instance.mdwamp(wamp, sessionEstablished: info)

Related

how to use the objective c framework class in swift project? [duplicate]

This question already has answers here:
How do I call Objective-C code from Swift?
(17 answers)
Closed 4 years ago.
As I developed the framework which is having both swift and objective c files.When I accessing the swift class in swift project it is working fine but when I tried to access the objective c class in swift i can't able to access and I also bridged the objective c file in swift project.can anyone help me to solve this above problem.
//swift project
import ObjCSwiftFramework
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let callingswiftfile = SwiftFile() //calling the swift file
let callingobjectivecfile = ObjectiveCFile()//**Use of unresolved identifier 'ObjectiveCFile'**
}
}
//Bridge file
#ifndef ObjCSwift_project_Bridging_Header_h
#define ObjCSwift_project_Bridging_Header_h
#import "ObjCSwiftFramework/ObjCSwiftFramework.h"
#endif
I also added the framework screen shot for your reference
for using objective-c files in swift you need to us Bridging header and now just simply Import the class name in that as below
Name of bridging file is like AppName-Bidging-Header.h
Create a Bridging Header
https://mycodetips.com/ios/manually-adding-swift-bridging-header-1290.html
File will look like after adding and importing obj-c class
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "CardIO.h"
you need to use bridging header. To know how to create a bridging header use below link
https://mycodetips.com/ios/manually-adding-swift-bridging-header-1290.html

What is missing after Bridging Obj-C to Swift

I have done this before, normally without trouble, setting the bridging header, etc.
This time, I tried to import a Obj-C framwork (github.com/jensmeder/FSKModem/) into my swift project. When typing code I can "see" all the Obj-C methods.
But when I compile the code, I get errors like "Use of undeclared identifier 'delete'" or "Use of undeclared identifier 'new'". Example of code in a .m file that is giving the error:
_audioFormat = new AudioStreamBasicDescription();
I'm familiar with Swift and don't know about Obj-C but I guess "delete" or "new" should exist, right? Do they belong to some framework that I should add?
I can compile the original code in Obj-C without trouble, but I need to incorporate it in my project that is written in Swift.
Many thanks for any help
Some more information...
The following functions are inside the .m file. Do you know why the "new" and "delete" keywords are unrecognized? This works fine when compiled as a normal "Obj-C" project. The error appears in Swift project only (after bridging headers of course):
-(void)dealloc
{
[self disconnect:NULL];
if (_audioFormat)
{
delete _audioFormat;
}
}
-(void) setupAudioFormat
{
_audioFormat = new AudioStreamBasicDescription();
//...
}
Should I add some #include that I might be missing, besides Foundation?
new and delete are C++ keywords; you can use them in C++ files (usually .cpp or .cc) and in Objective-C++ files (always .mm). You cannot use them in Swift (.swift) or in Objective-C (.m) files.
It is perfectly valid to use an Objective-C class from an Objective-C++ file from Swift; you can use Objective-C to wrap C++ classes for Swift usage. However it is not valid to use C++ from plain Objective-C.
Quite probably you just need to rename your Objective-C file to .mm.
new and delete are keywords from C++ not from Swift or Objective-C.
Swift
let audioFormat = AudioStreamBasicDescription()
Objective-C
AudioStreamBasicDescription* audioFormat = [[AudioStreamBasicDescription alloc] init];
In Swift, you simply need to call the object initializer when creating an object.
_audioFormat = AudioStreamBasicDescription()
The new keyword is an Objective-C combo of alloc and init.

How to import swift file into objective-c file in a swift project

The situation is a little complicated. I have a Swift project, and I have imported some Objective-C files into this project. Now, I want to use some Swift classes in these Objective-C files. How to do that?
It seems this reference covers everything:
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
Specifically:
Importing Swift into Objective-C
When you import Swift code into Objective-C, you rely on an
Xcode-generated header file to expose those files to Objective-C. This
automatically generated file is an Objective-C header that declares
the Swift interfaces in your target. It can be thought of as an
umbrella header for your Swift code. The name of this header is your
product module name followed by adding "-Swift.h". (You’ll learn more
about the product module name later, in Naming Your Product Module.)
Example:
Foo.swift in product named ExampleProduct
#objc public class Bar: NSObject {
var x: Int = 3
}
Blah.m in same product:
#import <Foundation/Foundation.h>
#import <ExampleProduct-Swift.h>
void doStuff() {
Bar *y = [[Bar alloc] init];
printf("%ld\n", (long)y.x);
}
It's even easy to call doStuff() from a Swift file if you create an Objective-C header file that defines the function prototype and then import that header file in the bridging header.
There's no limit to jumping back and forth.
Based on the comments it looks like you're having trouble importing the reverse bridging header.
Try this in terminal to ensure you're naming the file correctly:
% cd ~/Library/Developer/Xcode; find . -name '*-Swift.h'
I get (scroll all the way to the right):
./DerivedData/ExampleProduct-avoxifngmebkkqgndldocildsfcm/Build/Intermediates/ExampleProduct.build/Debug-iphonesimulator/ExampleProduct.build/DerivedSources/ExampleProduct-Swift.h
./DerivedData/ExampleProduct-avoxifngmebkkqgndldocildsfcm/Build/Intermediates/ExampleProduct.build/Debug-iphonesimulator/ExampleProduct.build/Objects-normal/x86_64/ExampleProduct-Swift.h
./DerivedData/ExampleProduct-avoxifngmebkkqgndldocildsfcm/Build/Intermediates/ExampleProduct.build/Debug-iphonesimulator/ExampleProductTests.build/DerivedSources/ExampleProductTests-Swift.h
./DerivedData/ExampleProduct-avoxifngmebkkqgndldocildsfcm/Build/Intermediates/ExampleProduct.build/Debug-iphonesimulator/ExampleProductTests.build/Objects-normal/x86_64/ExampleProductTests-Swift.h
Also, potentially a dup of:
How to call Objective-C code from Swift
With 326 upvotes that's worth studying!

Umbrella header file not updating after changing any swift code

I have a Mix project of Swift and Objective C. Initially I created a the project choosing Swift as language.
I have Objective C Bridging headers working absolutely fine - as I add any new obj c code that I manually update in Header file.
I imported "ProjectName-Swift.h" file in obj c and It worked fine. Now when I change any code in my swift files or add new swift files I can see "ProjectName-Swift.h" file is not updated with new changes. I tried to manually change the file and it worked.
I cleaned the project and rebuilt but no luck. I deleted the generated "ProjectName-Swift.h" file and tried building but still new changes are not shown.
SWIFT_CLASS("_TtC8MyProject13Type2")
#interface Type2 : NSObject
+ (Type2 *)sharedInstance;
- (float)method1:(Type2 *)entity amount:(float)amount;
- (float)method2:(Type1 *)category amount:(float)amount;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
#end
Any solution for this problem?
I know this was a long time ago, but I just struggled with the same problem when adding a protocol to my Swift code, and it wasn't being added to the -Swift.h header file.
The problem was my protocol wasn't marked as Public. I changed my protocol from this:
#objc protocol MyProtocol { //etc.... }
to this:
#objc public protocol MyProtocol { //etc.... }

Why won't Objective C classes autocomplete in swift? (updated)

I am importing my Objective C classes in a bridging header file into Swift:
Bridging Header
#import "MyCustomClass.h"
In my swift class, I am trying to use this code:
var x = MyCustomClass()
x.myFunMethod()
However, before I run/build I get "Use of unresolved identifier" errors at the above two lines. Furthermore, I cannot use autocomplete to type out x.myFunMethod(). Once I run/build, the errors go away and the app runs as normal.

Resources