I'm not able to import Swift class into Objective-C.
Xcode version: 10.1
Swift: 4.2
My steps:
Create empty Single-View Objective-C project called "test".
Create Swift class (mytestclass.swift)
Create bridging empty header "test-Swift.h"
Try to use in ViewController.m like this: #import "test-Swift.h"
[[MyTestClass init] calculate];
mytestclass.swift
import Foundation
#objc public class MyTestClass: NSObject {
#objc public func calculate() {
print("TEST")
}
}
After compilation I get:
/Users/xxx/Desktop/test/ViewController.m:19:7: Use of undeclared
identifier 'MyTestClass'
What am I missing?
Your problem is in step
3- Create bridging empty header "test-Swift.h"
You shouldn't create that file manually , as it's created when you add the swift file to your Objective-C , also you need to replace this line
[[MyTestClass init] calculate];
with
[[[MyTestClass alloc] init] calculate];
or
[[MyTestClass new] calculate];
Here is a working demo
Related
I'm merging my Swift project with already existing Objective-C code. I need to call some important methods of Swift class from objective C app delegate. I tried all methods given in net, but it was no use. Can any one help me out?
Yes, it's possible but with some limitations.
You can use only classes which inherited from NSObject, with public attribute and marked with #objc. At Objective-C code you should import "ProductModuleName-Swift.h" file which generated by compiler.
Here is an example of Swift class:
import Foundation
#objc public class ExampleClass: NSObject {
#objc public var someInstanceProperty = "Property"
#objc public func someFunction() {
print("Some function")
}
}
Notice that this class inherited from NSObject and have #objc and public attributes. After command+B you can take a look at generated bridge header through Assistance editor:
Then you should import the bridge header at your Objective-C class.
#import "ProductModuleName-Swift.h"
And then you can use your Swift class at Objective-C code like any other Objective-C class:
__auto_type const someClass = [ExampleClass new];
[someClass someFunction];
NSLog(#"%#", someClass.someInstanceProperty);
Here is an additional information from Apple:
Importing Swift into Objective-C
I'm using a Swift file, a sub-class of UIViewController in a project which is basically built previously in Objective-C. The bridging-header file is added by Xcode. I'm getting following error:
Cannot find protocol declaration for 'AVCapturePhotoCaptureDelegate'
This is my VC:
import UIKit
import AVFoundation
class ScanTargetAVFViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ScanTargetAVFViewController: AVCapturePhotoCaptureDelegate {
}
EDIT:
I've tried removing #import "MyProject-Swift.h" from one of the objective-c file and it works now. However, I need to reference Swift code from my Objective-C files. Any clue?
Try importing the MyProject-Swift.h file into MyObjectiveC.m rather than MyObjectiveC.h. If there are Swift classes you need to reference in MyObjectiveC.h you can use the class directive.
#class MySwiftClass;
Method in Swift extension of an Objective-C class can not be access from .m file
I have a bridge header:
extension foViewController {
public func gome()
{
print("Hi");
}
}
I can't call method like [self gome]; in viewdidload of .m file
To access Swift files in Objective-C classes, we have to add an import.
For example, if the project name is "De", add:
#import "De-Swift.h"
Only then we can access Swift files in Objective-C.
I have a swift project and i import a singleton, objective-c coded class in the project.
I tried to import the productname_swift.h file but no luck.
How can i access swift class in that singleton class?
Project made in Swift : To use Swift class in Objective C
To use Swift class in Objective C , follow given steps :
Create one Objective C class named User.
A popup display with "Would You like to configure an Objective-C bridging Header". Choose Create Bridging Header.
User.h
#import <Foundation/Foundation.h>
#interface User : NSObject
+(id) sharedUser ;
#end
User.m
#import "User.h"
#import "SwiftInObjectiveC-swift.h"
#implementation User
//Singleton of User
+(id)sharedUser{
static User *sharedUser = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedUser = [[self alloc] init];
//Class Method of ViewController.swift
[ViewController mySwiftClassFunction];
//Instance Method of ViewController.swift
ViewController *vc = [[ViewController alloc] init];
[vc mySwiftFunction];
});
return sharedUser;
}
-(void) myObjcectivecMethod {
ViewController *vc = [[ViewController alloc] init];
[vc mySwiftFunction];
}
Add #objc in your .swift class in front of Class name.
import UIKit
#objc class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func mySwiftFunction() {
print("Swift Function")
}
class func mySwiftClassFunction (){
print("Swift Class Function")
}
}
Go to Build Settings.
Set Product Module Name : ProjectName
Set Defines Module : YES
Set Embedded Content Contains Swift : YES
Set Install Objective-C Compatibility Header : YES
Set Objective-C Bridging Header : SwiftInObjectiveC/SwiftInObjectiveC-Bridging-Header.h
Import Auto generated header "ProjectName-swift.h" in your *.m file.
Clean and Run your Project.
It will work!!!
Follow Apple link for Mix and Match for detailed information.
It is very simple use Swift classes in Objective-C. Just follow steps given below
Open your Objective-C project.
Add a new swift file to the project.
One default dialogue will open to create a bridging header (If it
does not open by default, add a bridging header file).
Go to build settings type Swift Compiler and you will see Your
ProjectName-Swift.h in Swift Compiler- General like below
Import you desired swift classes build and run.
Adding multiple Swift files to an Objective-C project.
Let’s say you want to add Class1.swift, Class2.swift, and Class3.swift file to the SwiftToObjC project
Add Class1.swift file to the project.
XCode will ask "Would You like to configure an Objective-C bridging Header"? You choose "Create Bridging Header". So the SwiftToObjC-Bridging-Header.h file as well as the SwiftToObjC-Swift.h file are generated.
In the SwiftToObjC-Bridging-Header.h file you add this line
#class Class1;
When you add Class2.swift and Class3.swift to the project, you also add these two classes to the SwiftToObjC-Bridging-Header.h file as follows.
#class Class1;
#class Class2;
#class Class3;
5. In the target SwiftToObjC, under Build Settings, Swift-Compiler General you should see these two settings:
Objective-C Bridging Header -> SwiftToObjC-Bridging-Header.h
Objective-C Generated Interface Header Name -> SwiftToObjC-Swift.h
6. In the target SwiftToObjC, under Build Settings, Define Module is set to YES.
If you want to access Class1 in say MyViewController, then in MyViewController.m, you should import.
#import "SwiftToObjC-Bridging-Header.h"
In your Class1.swift file, declare your class as “#objc public” as follows. The same #objc public is used for Class2 and Class3.
#objc public class Class1: UITableViewController
Note that: UITableViewController is only an example for demonstration purpose.
That’s all.
I've read this question: How to call Objective-C code from Swift1. But I can found what I need.
I'm using swift like first language in my project and later I've add Objective C. But my problem is in Objective c file how I can call a function in swift, now I can do only the contrary (working in swift and using objective c class).
When I following code :
#import "ViewController-Swift.h"
xcode issues is that: file not found.
That's my problem now:
#objc class ShareViewController: UIViewController {
var name: String
init(name: String) {
self.name = name
}
// Needed to add a class level initializer
class func newInstanceNamed(name: String) -> ShareViewController {
return ShareViewController(name: name)
}
func abilitaTimer(){
println("ciao")
}
}
}
xcode issues is that: required initializer init coder...
Unless the project you are building is called ViewController, then you have the wrong file name. Look at the bridging header file that was created for you. If it is called xyzzy-Bridging-Header.h, then the file you want to import in Objective-C is xyzzy-Swift.h.
Swift migrate in Objc-based project:
Create new *.swift file (in Xcode) or add it by using Finder
Add swift bridging empty header if Xcode have not done this before (see 4 below)
Implement your Swift class by using #objc attribute:
import UIKit
#objc class Hello {
func sayHello () {
println("Hi there!")
}
}
Open Build Settings and check following parameters:
Product Module Name : myproject
Defines Module : YES
Embedded Content Contains Swift : YES
Install Objective-C Compatibility Header : YES
Objective-C Bridging Header : $(SRCROOT)/Sources/SwiftBridging.h
Import header (which is auto generated by Xcode) in your *.m file. Ex Suppose your project name is myproject then that file will be myproject-Swift.h.
#import "myproject-Swift.h"
Clean and rebuild your Xcode project