'Get computer informations from Swift [closed]
Solution 1:[1]
You should only ask one question at a time but I'll try to summarize:
To get the operating system version
ProcessInfo.processInfo.operatingSystemVersionStringTo get the machine type will not be so easy. You can get only the Model Identifier AFAIK
MacPro5,1. You would need to use the terminal commandsysctl hw.modelProcessor I think you will need the terminal command as well.
sysctl -n machdep.cpu.brand_stringsysctl -n machdep.cpu.core_countTo get the physical memory. I am not sure if you can get the free memory as well.
ProcessInfo.processInfo.physicalMemory
To use the terminal command in Swift you can do something like:
extension DataProtocol {
var string: String? { String(bytes: self, encoding: .utf8) }
}
extension Process {
static func stringFromTerminal(command: String) -> String {
let task = Process()
let pipe = Pipe()
task.standardOutput = pipe
task.launchPath = "/bin/bash"
task.arguments = ["-c", "sysctl -n " + command]
task.launch()
return pipe.fileHandleForReading.availableData.string ?? ""
}
static let processor = stringFromTerminal(command: "machdep.cpu.brand_string")
static let cores = stringFromTerminal(command: "machdep.cpu.core_count")
static let threads = stringFromTerminal(command: "machdep.cpu.thread_count")
static let vendor = stringFromTerminal(command: "machdep.cpu.vendor")
static let family = stringFromTerminal(command: "machdep.cpu.family")
}
Usage:
Process.processor
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

