swift语言混编--语言交互的接口

FFI stands for Foreign Function Interface. A foreign function interface is the popular name for the interface that allows code written in one language to call code written in another language.

https://www.cnblogs.com/feng9exe/p/10396004.html

 

-Bridging-Header.h

 

Alternatively, you can create a bridging header yourself by choosing File > New > File > [operating system] > Source > Header File.

 

in Swift Compiler make sure the Objective-C Bridging Header build setting has a path to the bridging header file. 

 

Any public Objective-C headers listed in the bridging header are visible to Swift. The Objective-C declarations are automatically available from any Swift file within that target, with no import statements. 

 

 

Import Code Within a Framework Target

To use the Objective-C declarations in files in the same framework target as your Swift code, you’ll need to import those files into the Objective-C umbrella header—the master header for your framework. Import your Objective-C files by configuring the umbrella header:

  1. Under Build Settings, in Packaging, make sure the Defines Module setting for the framework target is set to Yes.
  2. In the umbrella header, import every Objective-C header you want to expose to Swift.

Swift sees every header you expose publicly in your umbrella header. The contents of the Objective-C files in that framework are automatically available from any Swift file within that framework target, with no import statements. 

 

-Swift.h

 

Objective-C generated interface name header

 

This file is an Objective-C header that declares the Swift interfaces in your target, and you can think of it as an umbrella header for your Swift code. You don’t need to do anything special to create the generated header—just import it to use its contents in your Objective-C code.

 

By default, the generated header contains interfaces for Swift declarations marked with the public or open modifier. 

 

The Swift interfaces in the generated header include references to all of the Objective-C types used in them, so make sure to import the Objective-C headers for those types first.

 

 

Import Swift code into Objective-C within the same framework:

  1. Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes.
  2. Import the Swift code from that framework target into any Objective-C .m file within that target using this syntax and substituting the appropriate names:

#import <ProductName/ProductModuleName-Swift.h>

 

 

Swift3.0 调用C函数-_silen_name

 

创建.swift文件,引用.c文件函数

// 在全局作用域下进行声明

// swift 2.2使用关键字@asmname

import Foundation

@_silen_name("add") func c_add(_ num1: Int, _ num2: Int) -> Int

@_silgen_name("mul") func c_mul(_ num: Int, _ times: Int) -> Int

 

// 调用函数

print(c_add(90, 23))

print(c_mul(3, 20))

 

 

但从长远来看,Swift应该会很快集成一个FFI之类的功能以便于和其他语言/库集成,同时性能优化和对语言中的小问题做修正,完全代替Objective-C应该是在Apple的计划当中的,话说已经有那么多人为了Apple学了Objective-C,再多学一门语言也不是什么事儿。

 

https://segmentfault.com/q/1010000000533328

 

posted @ 2019-03-19 16:49  zzfx  阅读(286)  评论(0编辑  收藏  举报