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