In your case you are trying to retrive an integer value using code made t retrive string values, so it's not going to work.
Basically sysctl gives you 2 types of values (except for the value tables), which are integers and strings.
Here is some code i made for my library SwiftCPUDetect to retrive both:
//Strings
func getString(_ name: String) -> String?{
var size: size_t = 0
//gets the string size
var res = sysctlbyname(name, nil, &size, nil, 0)
if res != 0 {
//Returns nil if the specified name entry has not been found
return nil
}
//Allocates the appropriate vector (with extra termination just t be sure)
var ret = [CChar].init(repeating: 0, count: size + 1)
//retrives value
res = sysctlbyname(name, &ret, &size, nil, 0)
return res == 0 ? String(cString: ret) : nil
}
//Integers
func getInteger<T: FixedWidthInteger>(_ name: String) -> T?{
var ret = T()
var size = MemoryLayout.size(ofValue: ret) //gets the size of the provided integer value
let res = sysctlbyname(name, &ret, &size, nil, 0) //fetches the value
return res == 0 ? ret : nil //returns the retrieved integer if the value name entry exists otherwise returns nil
}
And here are some example usages (working only on macOS):
///Gets the number of cores of the current CPU
func cores_count() -> UInt?{
return getInteger("machdep.cpu.core_count")
}
///Gets the brand name for the current CPU
func brand_string() -> String?{
return getString("machdep.cpu.brand_string")
}
Also sysctl can do boolean values using integers set to either 0 or 1, so you can use this simple function to retrive boolean values too:
//Boolean
func getBool(_ name: String) -> Bool?{
guard let res: Int32 = getInteger(name) else{
return nil
}
return res == 1
}
I hope this can be useful to you, and feel free to use the library i mentioned in your projects, it contains a lot of useful stuff like this.