gpt4 book ai didi

ios - 将 Int 传递给 C 函数

转载 作者:搜寻专家 更新时间:2023-11-01 06:43:58 24 4
gpt4 key购买 nike

我正在编写 Swift 代码并且需要调用 C 函数。

var idx = 5
//ERROR: Cannot invoke c_func with argument list of type '(Int)'
c_func(idx)

C 函数签名是:

void c_func(int idx)

所以,我的问题是如何将 swift Int 传递给接受 int 作为参数的 C 函数?

最佳答案

C 类型 int 映射到 Swift 为

/// The C 'int' type.
typealias CInt = Int32

因此您必须将变量声明为CInt:

var idx : CInt = 5
c_func(idx)

或者将Int转换为CInt:

var idx = 5
c_func(CInt(idx))

(当然你可以用Int32来代替,如果你想强调它是一个固定大小的整数类型。)

有关(目标-)C 类型之间映射的更多信息和 Swift 类型可以在 "Using Swift with Cocoa and Objective-C" 中找到文档。

关于ios - 将 Int 传递给 C 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32478352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com