Hi,
I need help to solve this error. Basically this was working fine in Swift 2.x and Swift 3 broken. Please advise.
Note that below code is almost converted to Swift 3 but with below error.
Error
'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.
Error Line: 7
let kerr = task_info(name, flavor, UnsafeMutablePointer(infoPointer), &size)
Here is the code snippet
open class func getMemoryUsed() -> Float {
let MACH_TASK_BASIC_INFO_COUNT = (MemoryLayout<mach_task_basic_info_data_t>.size / MemoryLayout<natural_t>.size)
let name = mach_task_self_
let flavor = task_flavor_t(MACH_TASK_BASIC_INFO)
var size = mach_msg_type_number_t(MACH_TASK_BASIC_INFO_COUNT)
let infoPointer = UnsafeMutablePointer<mach_task_basic_info>.allocate(capacity: 1)
let kerr = task_info(name, flavor, UnsafeMutablePointer(infoPointer), &size)
let info = infoPointer.move()
infoPointer.deallocate(capacity: 1)
if kerr == KERN_SUCCESS {
let used_bytes: Float = Float(info.resident_size)
let total_bytes: Float = Float(ProcessInfo.processInfo.physicalMemory)
log.info("Used: \(used_bytes / 1024.0 / 1024.0) MB out of \(total_bytes / 1024.0 / 1024.0) MB (\(used_bytes * 100.0 / total_bytes)%)")
return used_bytes / 1000000 /
}
}