gpt4 book ai didi

ios - 如何访问 UnsafeMutablePointer 结构成员

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

let cfg: UnsafeMutablePointer <pjsua_config> = nil;
pjsua_config_default(cfg);
cfg.cb // Error: Value of type 'UnsafeMutablePointer<pjsua_config>' has no member 'cb'

如何转换 cfg 来访问它的字段?我试图在 Apple 的 'Interacting with C API' document 中找到答案但没有与此问题相关的信息。

最佳答案

其实《Interacting with C APIs》里面相关的资料章节:

When a function is declared as taking an UnsafeMutablePointer<Type> argument, it can accept any of the following:

  • ...
  • An inout expression whose operand is a stored lvalue of type Type, which is passed as the address of the lvalue
  • ...

所以你可以简单地传递一个(初始化的)pjsua_config 的地址使用 & 作为“输入参数”结构,类似于您在 C 中所做的:

var  cfg = pjsua_config() // creates a `struct pjsua_config` with all fields set to zero
pjsua_config_default(&cfg)

现在您可以访问cfg.cb ,你不必担心内存管理。

关于ios - 如何访问 UnsafeMutablePointer 结构成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33525608/

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