如何在objective-c文件中访问swift类

时间:2023-01-15 15:03:29

I have a swift project and i import a singleton, objective-c coded class in the project.

我有一个swift项目,我在项目中导入一个单例、objective-c代码类。

I tried to import the productname_swift.h file but no luck.

我试图导入productname_swift。h文件,但运气不好。

How can i access swift class in that singleton class?

如何访问该单例类中的swift类?

1 个解决方案

#1


2  

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

Swift项目:在Objective C中使用Swift类。

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

要在Objective C中使用Swift类,请按照以下步骤:

  1. Create one Objective C class named User.
  2. 创建一个名为User的目标C类。
  3. A popup display with "Would You like to configure an Objective-C bridging Header". Choose Create Bridging Header.
  4. 弹出式显示:“您想要配置Objective-C桥接头吗?”选择创建连接头。

如何在objective-c文件中访问swift类

User.h

User.h

#import <Foundation/Foundation.h>

@interface User : NSObject

+(id) sharedUser ;

@end

User.m

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.

    在您的.swift类中在类名前面添加@objc。

     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

    设置产品模块名:ProjectName

如何在objective-c文件中访问swift类

  1. Set Defines Module : YES
  2. 集合定义模块:是的

如何在objective-c文件中访问swift类

  1. Set Embedded Content Contains Swift : YES
  2. 嵌入内容包含Swift:是的。

如何在objective-c文件中访问swift类

  1. Set Install Objective-C Compatibility Header : YES
  2. 设置安装Objective-C兼容头:是的

如何在objective-c文件中访问swift类

  1. Set Objective-C Bridging Header : SwiftInObjectiveC/SwiftInObjectiveC-Bridging-Header.h
  2. 设置Objective-C桥接头:SwiftInObjectiveC/ swiftinobjective -Bridging-Header.h

如何在objective-c文件中访问swift类

  1. Import Auto generated header "ProjectName-swift.h" in your *.m file.
  2. 导入自动生成的标题"项目名称-swift。* h”。m文件。
  3. Clean and Run your Project.
  4. 清理并运行项目。
  5. It will work!!!
  6. 它将工作! ! !

Follow Apple link for Mix and Match for detailed information.

根据苹果链接进行混合和匹配以获得详细信息。

如何在objective-c文件中访问swift类

#1


2  

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

Swift项目:在Objective C中使用Swift类。

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

要在Objective C中使用Swift类,请按照以下步骤:

  1. Create one Objective C class named User.
  2. 创建一个名为User的目标C类。
  3. A popup display with "Would You like to configure an Objective-C bridging Header". Choose Create Bridging Header.
  4. 弹出式显示:“您想要配置Objective-C桥接头吗?”选择创建连接头。

如何在objective-c文件中访问swift类

User.h

User.h

#import <Foundation/Foundation.h>

@interface User : NSObject

+(id) sharedUser ;

@end

User.m

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.

    在您的.swift类中在类名前面添加@objc。

     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

    设置产品模块名:ProjectName

如何在objective-c文件中访问swift类

  1. Set Defines Module : YES
  2. 集合定义模块:是的

如何在objective-c文件中访问swift类

  1. Set Embedded Content Contains Swift : YES
  2. 嵌入内容包含Swift:是的。

如何在objective-c文件中访问swift类

  1. Set Install Objective-C Compatibility Header : YES
  2. 设置安装Objective-C兼容头:是的

如何在objective-c文件中访问swift类

  1. Set Objective-C Bridging Header : SwiftInObjectiveC/SwiftInObjectiveC-Bridging-Header.h
  2. 设置Objective-C桥接头:SwiftInObjectiveC/ swiftinobjective -Bridging-Header.h

如何在objective-c文件中访问swift类

  1. Import Auto generated header "ProjectName-swift.h" in your *.m file.
  2. 导入自动生成的标题"项目名称-swift。* h”。m文件。
  3. Clean and Run your Project.
  4. 清理并运行项目。
  5. It will work!!!
  6. 它将工作! ! !

Follow Apple link for Mix and Match for detailed information.

根据苹果链接进行混合和匹配以获得详细信息。

如何在objective-c文件中访问swift类