Preview Devices

有了下列的 extension:

import SwiftUI

extension PreviewDevice {
    // ⭐️ `PreviewDevice` conforms to `ExpressibleByStringLiteral`
    static let iPhone13ProMax: PreviewDevice = "iPhone 13 Pro Max"
    static let iPhone13Pro   : PreviewDevice = "iPhone 13 Pro"
}

就可以使用下列語法:

.previewDevice(.iPhone13Pro)    // ⭐️ use static member

使用下列 extension:

extension View {
    /// .previewDevice(deviceName)
    public func previewDevice(_ deviceName: String) -> some View {
        previewDevice(PreviewDevice(rawValue: deviceName))
    }
}

則可用下列語法:

// string constant
let deviceName = "iPhone X"
.previewDevice(deviceName)    // ⭐️ not a string literal

Last updated