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
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM