作者:冬日暖光816 | 来源:互联网 | 2023-01-15 17:32
我正在尝试在Xcode 9的目标c项目中添加swift 4文件。为此,我正在按照以下步骤操作-
步骤1:假设swift类名称为ShowAlertSwift.swift
import UIKit
@objc public class ShowAlertSwift: NSObject {
@objc func printSome() {
print("Print line System")
}
@objc func test () {
print("Test swift Class in objective C project")
}
}
步骤2:目标-c类首先声明@class ShowAlertSwift
在objectiveClass.h中,我遵循了以下步骤
@class ShowAlertSwift
@interface ShowAlertSwift: NSObject
-(void)test;
-(void)printSome;
@end
@interface ViewController : UIViewController
@end
第3步-在ObjectiveClass.m中
#import "myproject-swift.h"
@interface UIViewController ()
{
}
@implementation
- (void)viewDidLoad
{
ShowAlertSwift *alertObj = [[ShowAlertSwift alloc]init];
[alertObj test];
}
我还设置了以下属性
Defines modules - Yes
Embedded_Content_Contains_Swift - Yes
Install Objective-C compatibility header - Yes
Objective C bridging header - $(SRCROOT)/Sources/SwiftBridging.h
Product Module Name - projectName
毕竟这些东西我得到像
1. myproject-swift.h file not found
2. Consecutive statement on a line must be seprated by ';'
3. failed to emit precompiled header '/Users/xyz/pqr/Trunk/build/SharedPrecompiledHeaders/myproject-Bridging-Header-swift_1CBSOZ0HA1Z9R-clang_UESSBOJR5CSH.pch' for bridging header '/Users/xyz/pqr/Trunk/OpenPage2/com/myProject/login/Login_iPad/myProject-Bridging-Header.h'
谁能知道如何解决Swift 4中Xcode 9上的问题?
1> Shubham JAin..:
我已经做了很多事情来解决这个问题,但是我没有成功。但是由于我的奉献精神,我终于解决了这个问题-
注意:要在目标C代码中访问swift类,您必须在类语句中使用@objec关键字,例如
@objc public class ShowAlertSwift: NSObject
首先创建一个快速班
import UIKit
@objc public class ShowAlertSwift: NSObject {
@objc func printSome() {
print("Print line System")
}
@objc func test () {
print("Test swift Class in objective C project")
}
}
在objectiveClass.h中,我遵循了以下步骤
@class ShowAlertSwift
@interface ViewController : UIViewController
@end
此处无需再次定义接口,因为“ myproject-swift.h”文件中已存在该接口。请先参阅问题以清楚了解或参阅下文
@interface ShowAlertSwift: NSObject
-(void)test;
-(void)printSome;
@end
第3步-在ObjectiveClass.m中
#import "myproject-swift.h"
@interface UIViewController ()
{
}
@implementation
- (void)viewDidLoad
{
ShowAlertSwift *alertObj = [[ShowAlertSwift alloc]init];
[alertObj test];
}
同时在构建设置中启用/更新这些属性-
1. Defines Module set to YES (Mandatory on project - not on target)
2. Embedded_Content_Contains_Swift - Yes
3. Install Objective-C compatibility header - Yes
4. Objective C bridging header - $(SRCROOT)/Sources/SwiftBridging.h
5. Product Module Name - projectName