Dragan Tech Dragan Tech - 22 days ago 6
iOS Question

Getting error on host_processor_sets swift 3

I don't know why i am getting this error

var list = [ProcessInfo]()
var psets = processor_set_name_array_t.allocate(capacity: 1)
var pcnt: mach_msg_type_number_t = 0

var result = host_processor_sets(machHost, &psets, &pcnt)


the last statement gives :


cannot pass immutable value of type as inout argument

Answer

The function host_processor_sets is imported into Swift as:

func host_processor_sets(
    _ host_priv: host_priv_t,
    _ processor_sets: UnsafeMutablePointer<processor_set_name_array_t?>!,
    _ processor_setsCnt: UnsafeMutablePointer<mach_msg_type_number_t>!
) -> kern_return_t

(Taken from the Quick Help of Xcode.)

When you find an UnsafeMutablePointer<T>! in imported C-function, you usually declare a variable of type T and pass it as an inout parameter.

So, you need to declare variables of processor_set_name_array_t? and mach_msg_type_number_t.

var psets: processor_set_name_array_t? = nil
var pcnt: mach_msg_type_number_t = 0
var result = host_processor_sets(machHost, &psets, &pcnt)