iOS Objc和swift互相调用
新建一个objectivec工程
新建一个swift文件,提示要不要建一个bridge文件选择是
buildsettings里面enable swift
然后新建一个oc的类
#import <Foundation/Foundation.h> @interface ObjcObj : NSObject - (void)test; - (void)test1; @end #import "ObjcObj.h" #import "SwiftAndObjc-Swift.h" @implementation ObjcObj - (void)test { SwiftObj*swift = [SwiftObj new]; [swift callSwift:@"hi hello"]; [swift callSwift:nil]; } - (void)test1 { SwiftObj*swift = [SwiftObj new]; [swift callObjc]; } @end
这里头文件里面有个
#import "SwiftAndObjc-Swift.h"
左边是工程名,右边是Swift #import "工程名-Swift.h"
在swift文件里面加入代码
import Foundation @objc class SwiftObj:NSObject { var string:String; override init() { string = "hello swift called"; } func callSwift(str:String?) -> Void { if let s = str{ print(s); } else{ print(string); } } func callObjc()->Void{ let objc = ObjcObj(); objc.test(); } }
下面是在bridge文件里面加入头文件
下面调用触发: