Cela fonctionne avec Swift 4.
La différence la plus importante est la suivante : Il convertit en Int64 avant de multiplier, parce que sinon vous obtenez rapidement des débordements, surtout si vous l'exécutez dans un simulateur où il utilise la mémoire du PC.
var pagesize: vm_size_t = 0
let host_port: mach_port_t = mach_host_self()
var host_size: mach_msg_type_number_t = mach_msg_type_number_t(MemoryLayout<vm_statistics_data_t>.stride / MemoryLayout<integer_t>.stride)
host_page_size(host_port, &pagesize)
var vm_stat: vm_statistics = vm_statistics_data_t()
withUnsafeMutablePointer(to: &vm_stat) { (vmStatPointer) -> Void in
vmStatPointer.withMemoryRebound(to: integer_t.self, capacity: Int(host_size)) {
if (host_statistics(host_port, HOST_VM_INFO, $0, &host_size) != KERN_SUCCESS) {
NSLog("Error: Failed to fetch vm statistics")
}
}
}
/* Stats in bytes */
let mem_used: Int64 = Int64(vm_stat.active_count +
vm_stat.inactive_count +
vm_stat.wire_count) * Int64(pagesize)
let mem_free: Int64 = Int64(vm_stat.free_count) * Int64(pagesize)