我正在尝试将 ObjC stackoverflow 答案转换为 Swift 并失败。看起来我正在通过 a UnsafeMutablePointer<mach_msg_type_number_t>
,而我应该通过 an inout mach_msg_type_number_t
,但我似乎无法解决我的问题。根据我对 Swift 指针文档的理解(不多),这些应该是可以互换的..?
更多信息如下。
这是目标C:
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &size);
就我在 Swift 中得到的信息而言(多行以便于类型检查)
let name: task_name_t = mach_task_self_
let flavor: task_flavor_t = task_flavor_t(MACH_TASK_BASIC_INFO)
var info: mach_task_basic_info
var size: mach_msg_type_number_t = UnsignedFixed(sizeof(mach_task_basic_info_t))
let kerr = task_info(name, flavor, info as task_info_t, &size)
task_info
签名是:
func task_info(target_task: task_name_t, flavor: task_flavor_t, task_info_out: task_info_t, task_info_outCnt: UnsafeMutablePointer<mach_msg_type_number_t>) -> kern_return_t
最后一行的错误是:
Cannot convert the expression's type '(@!lvalue task_name_t, task_flavor_t, task_info_t, inout mach_msg_type_number_t)' to type 'kern_return_t'