在Objective-C中使用Swift类非常简单。只需按照下面给出的步骤
我有一个快速的项目,并且在该项目中导入了一个单例的,由目标C编码的类。
我尝试导入productname_swift.h文件,但是没有运气。
我如何访问该单例课程中的swift类别?
Swift中的项目:要在Objective C中使用Swift类
要使用Objective C中的Swift类],请按照给定的步骤进行:
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]; }
在.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") } }
转到构建设置。
设置产品模块名称
:项目名称在Objective-C中使用Swift类非常简单。只需按照下面给出的步骤
ProjectName-Swift.h
-如下所示在Objective-C中使用Swift类非常简单。只需按照下面给出的步骤