How to call C from Swift?

前端 未结 6 1599
夕颜
夕颜 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 10:54

    The compiler converts C API to Swift just like it does for Objective-C.

    import Cocoa
    
    let frame = CGRect(x: 10, y: 10, width: 100, height: 100)
    
    import Darwin
    
    for _ in 1..10 {
        println(rand() % 100)
    }
    

    See Interacting with Objective-C APIs in the docs.

提交回复
热议问题