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
.previewDevice() ่ฆๆฅๆถไธๅ PreviewDevice? ๅๆธ๏ผไฝๅฏไปฅไฝฟ็จ๏ผ
.previewDevice("iPhone XS Max")
็่ชๆณ๏ผๆฏๅ ็บ PreviewDevice conforms to ExpressibleByStringLiteral
ๅฆๆ็ดๆฅไฝฟ็จไธๅ่ชๆณๆ็ข็้ฏ่ชค๏ผ
.previewDevice(deviceName) // var deviceName = "iPhone X"
ๅ ็บ deviceName
ๆฏไธๅ String variable๏ผไธฆไธๆฏ String literalใ
ๆไปฅๅฆๆไธๅไปปไฝๆดๅ ๏ผๆๅๅช่ฝไฝฟ็จ๏ผ
.previewDevice(PreviewDevice(rawValue: deviceName))
SwiftUI Tutorials โฉ Sec. 8: Generate Previews Dynamically - Step 2
View โฉ .previewDevice(_:)
PreviewDevice (struct)
Last updated