Swift MacOS 知道OS 和 Xcode 的版本
Swift MacOS know the version of the OS and Xcode
我应该为 macOS、Xcode 和 OS 版本的程序打印。
例如:
OS: macOS 12.3
Xcode: 13.3
我该怎么办?
编辑:
也许我为 mac 版本做到了:
let osVersion = ProcessInfo.processInfo.operatingSystemVersion
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString("MacOS:
\(osVersion.majorVersion).
\(osVersion.minorVersion).
\(osVersion.patchVersion)", forType: .string)
对于macOS版本,确实可以从ProcessInfo.processInfo.operatingSystemVersion
获取。
对于Xcode版本,你可以先从它的bundle ID中找到Xcode的bundle在哪里,找到它的bundle,然后从它的bundle的Info.plist中得到版本为一个字符串。
guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.apple.dt.Xcode"),
let bundle = Bundle(url: url) else {
print("Xcode is not installed")
exit(1)
}
guard let infoDict = bundle.infoDictionary,
let version = infoDict["CFBundleShortVersionString"] as? String else {
print("No version found in Info.plist")
exit(1)
}
print(version) // Example output: 13.1
您可以将 let url = ...
步骤替换为 NSOpenPanel
提示,让用户选择他们的 Xcode 安装位置。
我应该为 macOS、Xcode 和 OS 版本的程序打印。
例如:
OS: macOS 12.3
Xcode: 13.3
我该怎么办?
编辑:
也许我为 mac 版本做到了:
let osVersion = ProcessInfo.processInfo.operatingSystemVersion
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString("MacOS:
\(osVersion.majorVersion).
\(osVersion.minorVersion).
\(osVersion.patchVersion)", forType: .string)
对于macOS版本,确实可以从ProcessInfo.processInfo.operatingSystemVersion
获取。
对于Xcode版本,你可以先从它的bundle ID中找到Xcode的bundle在哪里,找到它的bundle,然后从它的bundle的Info.plist中得到版本为一个字符串。
guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.apple.dt.Xcode"),
let bundle = Bundle(url: url) else {
print("Xcode is not installed")
exit(1)
}
guard let infoDict = bundle.infoDictionary,
let version = infoDict["CFBundleShortVersionString"] as? String else {
print("No version found in Info.plist")
exit(1)
}
print(version) // Example output: 13.1
您可以将 let url = ...
步骤替换为 NSOpenPanel
提示,让用户选择他们的 Xcode 安装位置。