6

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?

user3068613
  • 123
  • 2
  • 9

3 Answers3

5

Project made in Swift : To use Swift class in Objective C

To use Swift class in Objective C , follow given steps :

  1. Create one Objective C class named User.
  2. A popup display with "Would You like to configure an Objective-C bridging Header". Choose Create Bridging Header.

enter image description here

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];

}
  1. 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")
    
    }
    
    }
    
  2. Go to Build Settings.

  3. Set Product Module Name : ProjectName

enter image description here

  1. Set Defines Module : YES

enter image description here

  1. Set Embedded Content Contains Swift : YES

enter image description here

  1. Set Install Objective-C Compatibility Header : YES

enter image description here

  1. Set Objective-C Bridging Header : SwiftInObjectiveC/SwiftInObjectiveC-Bridging-Header.h

enter image description here

  1. Import Auto generated header "ProjectName-swift.h" in your *.m file.
  2. Clean and Run your Project.
  3. It will work!!!

Follow Apple link for Mix and Match for detailed information.

enter image description here

technerd
  • 14,144
  • 10
  • 61
  • 92
3

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 enter image description here

  • Import you desired swift classes build and run.

-1

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

  1. Add Class1.swift file to the project.

  2. 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.
  3. In the SwiftToObjC-Bridging-Header.h file you add this line

@class Class1;

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

  1. If you want to access Class1 in say MyViewController, then in MyViewController.m, you should import.


#import "SwiftToObjC-Bridging-Header.h"

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

Tim Le
  • 229
  • 3
  • 5
  • The way I understand it is that the key is the "-Swift.h" file. The compiler creates this file, as explained here: https://stackoverflow.com/a/24087280/3017386 And, as mentioned in one of the comments, here: http://ericasadun.com/2014/08/21/swift-calling-swift-functions-from-objective-c/ The "-Bridging-Header.h" file contains headers which are supposed to be exposed _towards_ Swift, not the other way around. – easytarget Nov 17 '21 at 22:18