ios oc调用swift framework

1.oc 调用swift

/*oc调用swift,  project name为DeomOC:
 1.oc工程DemoOC中显式创建一个swift文件,生成DemoOC-Bridging-Header.h
 2.#import "DemoOC-Swift.h"
 https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
*/
#import "DemoOC-Swift.h"

- (void)viewDidLoad {
    [super viewDidLoad];
    swifttest *tests = [swifttest new];
    NSLog(@"%@", [tests test]);
}


//  swifttest.swift
//  DemoOC
import Foundation

class swifttest:NSObject {
    func test() -> String {
        return "hi "
    }
}

2.调用swift framework

碰到问题及解决方法:ios dyld: Library not loaded: @rpath/xxx.framework/xxx 之根本原因

oc调用swift framework注意点:

  • class 需继承自NSObject,且public属性才能暴露给oc
  • 函数名转换规则:swift func foo(para1:...) -> oc fooWithPara1
  • oc不支持重载,但根据转换规则可以给第一个参数不同的名字,转换的函数名不一样就可以
  • 不支持struct的转换,需用 class
  • enum必须为整型才能转换
  • 函数返回值不支持tuple