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类,请按照以下步骤:
- Create one Objective C class named User.
- 创建一个名为User的目标C类。
- A popup display with "Would You like to configure an Objective-C bridging Header". Choose Create Bridging Header.
- 弹出式显示:“您想要配置Objective-C桥接头吗?”选择创建连接头。
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];
}
-
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") } }
-
Go to Build Settings.
去构建设置。
-
Set Product Module Name : ProjectName
设置产品模块名:ProjectName
- Set Defines Module : YES
- 集合定义模块:是的
- Set Embedded Content Contains Swift : YES
- 嵌入内容包含Swift:是的。
- Set Install Objective-C Compatibility Header : YES
- 设置安装Objective-C兼容头:是的
- Set Objective-C Bridging Header : SwiftInObjectiveC/SwiftInObjectiveC-Bridging-Header.h
- 设置Objective-C桥接头:SwiftInObjectiveC/ swiftinobjective -Bridging-Header.h
- Import Auto generated header "ProjectName-swift.h" in your *.m file.
- 导入自动生成的标题"项目名称-swift。* h”。m文件。
- Clean and Run your Project.
- 清理并运行项目。
- It will work!!!
- 它将工作! ! !
Follow Apple link for Mix and Match for detailed information.
根据苹果链接进行混合和匹配以获得详细信息。
#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类,请按照以下步骤:
- Create one Objective C class named User.
- 创建一个名为User的目标C类。
- A popup display with "Would You like to configure an Objective-C bridging Header". Choose Create Bridging Header.
- 弹出式显示:“您想要配置Objective-C桥接头吗?”选择创建连接头。
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];
}
-
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") } }
-
Go to Build Settings.
去构建设置。
-
Set Product Module Name : ProjectName
设置产品模块名:ProjectName
- Set Defines Module : YES
- 集合定义模块:是的
- Set Embedded Content Contains Swift : YES
- 嵌入内容包含Swift:是的。
- Set Install Objective-C Compatibility Header : YES
- 设置安装Objective-C兼容头:是的
- Set Objective-C Bridging Header : SwiftInObjectiveC/SwiftInObjectiveC-Bridging-Header.h
- 设置Objective-C桥接头:SwiftInObjectiveC/ swiftinobjective -Bridging-Header.h
- Import Auto generated header "ProjectName-swift.h" in your *.m file.
- 导入自动生成的标题"项目名称-swift。* h”。m文件。
- Clean and Run your Project.
- 清理并运行项目。
- It will work!!!
- 它将工作! ! !
Follow Apple link for Mix and Match for detailed information.
根据苹果链接进行混合和匹配以获得详细信息。