How to call C from Swift?

前端 未结 6 1586
夕颜
夕颜 2020-11-27 10:33

Is there a way to call C routines from Swift?

A lot of iOS / Apple libraries are C only and I\'d still like to be able to call those.

For example, I\'d like

6条回答
  •  离开以前
    2020-11-27 11:02

    Yes, you can of course interact with Apples C libraries. Here is explained how.
    Basically, the C types, C pointers, etc are translated into Swift objects, for example a C int in Swift is a CInt.

    I've build a tiny example, for another question, which can be used as a little explanation, on how to bridge between C and Swift:

    main.swift

    import Foundation
    
    var output: CInt = 0
    getInput(&output)
    
    println(output)
    

    UserInput.c

    #include 
    
    void getInput(int *output) {
        scanf("%i", output);
    }
    

    cliinput-Bridging-Header.h

    void getInput(int *output);
    

    Here is the original answer.

提交回复
热议问题