我正在尝试在 Xcode 9 上的 objective-c 项目中添加 swift 4 文件。为此,我正在执行以下步骤 -
第一步:假设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 上的问题?
最佳答案
我已经做了很多事情来解决这个问题,但我没有取得成功。但由于我的奉献精神,我终于解决了这个问题 -
Note : To access swift class in objective C code you have to use the @objec keyword along the class statement like that
@objc public class ShowAlertSwift: NSObject
首先创建一个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")
}
}
在 objectiveClass.h 中,我遵循了这些步骤
@class ShowAlertSwift
@interface ViewController : UIViewController
@end
此处无需再次定义接口(interface),因为该接口(interface)已经存在于“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];
}
同时在build设置中启用/更新这些属性 -
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
关于objective-c - 使用 Swift 4 在 Xcode 9 上找不到“ProjectName-Swift.h”文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46541792/