Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access swift class in objective-c file

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?

like image 303
user3068613 Avatar asked Jan 19 '16 15:01

user3068613


2 Answers

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

like image 94
technerd Avatar answered Sep 19 '22 01:09

technerd


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.

like image 43
Mohammad Kamran Usmani Avatar answered Sep 21 '22 01:09

Mohammad Kamran Usmani