Xcode 11 ? Swift 5.1
extension CGDirectDisplayID {
var displayMode: CGDisplayMode? { CGDisplayCopyDisplayMode(self) }
func allDisplayModes(options: CFDictionary? = nil) -> [CGDisplayMode] { CGDisplayCopyAllDisplayModes(self, options) as? [CGDisplayMode] ?? [] }
}
extension CGDisplayMode {
var resolution: String { .init(width) + " x " + .init(height) }
}
struct Display {
static var main: CGDirectDisplayID { CGMainDisplayID() }
static var mode: CGDisplayMode? { main.displayMode }
static func allModes(for directDisplayID: CGDirectDisplayID = main) -> [CGDisplayMode] { directDisplayID.allDisplayModes() }
}
Usage:
if let resolution = Display.mode?.resolution {
print("Resolution:", resolution)
}
for mode in Display.allModes() {
print(mode.resolution)
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…