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