我正在尝试在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")
}
}
Run Code Online (Sandbox Code Playgroud)
步骤2:目标-c类首先声明@class ShowAlertSwift
在objectiveClass.h中,我遵循了以下步骤
@class ShowAlertSwift
@interface ShowAlertSwift: NSObject
-(void)test;
-(void)printSome;
@end
@interface ViewController : UIViewController
@end
Run Code Online (Sandbox Code Playgroud)
第3步-在ObjectiveClass.m中
#import "myproject-swift.h"
@interface UIViewController ()
{
}
@implementation
- (void)viewDidLoad
{
ShowAlertSwift *alertObj = [[ShowAlertSwift alloc]init];
[alertObj test];
}
Run Code Online (Sandbox Code Playgroud)
我还设置了以下属性
Defines modules - Yes
Embedded_Content_Contains_Swift - Yes …Run Code Online (Sandbox Code Playgroud) 假设有两个类,一个在 swift 中,另一个在同一个项目中的objective-c 类中。
在 swift 类中,我声明了委托,我想在目标 c 类中设置该委托。
我已经这样做了以下方式...
import UIKit
@objc public protocol SwiftLoginVCDelegate: class {
}
@objc public class SwiftLoginViewController: UIViewController {
@IBOutlet var txtUserName: UITextField!
@IBOutlet var txtPassword: UITextField!
@IBOutlet var btnLogin: UIButton!
var delegate: SwiftLoginVCDelegate?
override public func viewDidLoad() {
super.viewDidLoad()
self.txtPassword.text = "XYZ"
self.txtPassword.text = "123"
self.btnLogin.backgroundColor = UIColor.blue
}
@objc public func testObjective(){
print("Swift class is integrated in objective c project")
}
Run Code Online (Sandbox Code Playgroud)
目标 c 类是
#import "Mediator.h"
@class SwiftLoginViewController;
@protocol SwiftLoginVCDelegate;
@interface LoginMediator …Run Code Online (Sandbox Code Playgroud)