简体   繁体   中英

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?

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 .

在此处输入图像描述

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

在此处输入图像描述

  1. Set Defines Module : YES

在此处输入图像描述

  1. Set Embedded Content Contains Swift : YES

在此处输入图像描述

  1. Set Install Objective-C Compatibility Header : YES

在此处输入图像描述

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

在此处输入图像描述

  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.

在此处输入图像描述

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

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM