Skip to content

Instantly share code, notes, and snippets.

@bennokress
Created February 12, 2021 22:04
Show Gist options
  • Save bennokress/675c63fd3d5854ea26cc3d558c4bc3ff to your computer and use it in GitHub Desktop.
Save bennokress/675c63fd3d5854ea26cc3d558c4bc3ff to your computer and use it in GitHub Desktop.
Add convenience to your SwiftUI Previews by using `.previewDevice(.iPhone12)` instead of initializing by String.
import SwiftUI
extension PreviewDevice: Identifiable {
public var id: String { rawValue } // Support for each loops over an array of Preview Devices
static var iPhoneSE1: PreviewDevice { .init(.iPhoneSE1) }
static var iPhone7: PreviewDevice { .init(.iPhone7) }
static var iPhone7Plus: PreviewDevice { .init(.iPhone7Plus) }
static var iPhone8: PreviewDevice { .init(.iPhone8) }
static var iPhone8Plus: PreviewDevice { .init(.iPhone8Plus) }
static var iPhoneX: PreviewDevice { .init(.iPhoneX) }
static var iPhoneXs: PreviewDevice { .init(.iPhoneXs) }
static var iPhoneXsMax: PreviewDevice { .init(.iPhoneXsMax) }
static var iPhoneXʀ: PreviewDevice { .init(.iPhoneXʀ) }
static var iPhone11: PreviewDevice { .init(.iPhone11) }
static var iPhone11Pro: PreviewDevice { .init(.iPhone11Pro) }
static var iPhone11ProMax: PreviewDevice { .init(.iPhone11ProMax) }
static var iPhoneSE2: PreviewDevice { .init(.iPhoneSE2) }
static var iPhone12mini: PreviewDevice { .init(.iPhone12mini) }
static var iPhone12: PreviewDevice { .init(.iPhone12) }
static var iPhone12Pro: PreviewDevice { .init(.iPhone12Pro) }
static var iPhone12ProMax: PreviewDevice { .init(.iPhone12ProMax) }
private enum iPhone {
case iPhoneSE1
case iPhone7
case iPhone7Plus
case iPhone8
case iPhone8Plus
case iPhoneX
case iPhoneXs
case iPhoneXsMax
case iPhoneXʀ
case iPhone11
case iPhone11Pro
case iPhone11ProMax
case iPhoneSE2
case iPhone12mini
case iPhone12
case iPhone12Pro
case iPhone12ProMax
}
private init(_ iPhone: iPhone) {
switch iPhone {
case .iPhoneSE1: self = PreviewDevice(rawValue: "iPhone8,4")
case .iPhone7: self = PreviewDevice(rawValue: "iPhone9,3")
case .iPhone7Plus: self = PreviewDevice(rawValue: "iPhone9,4")
case .iPhone8: self = PreviewDevice(rawValue: "iPhone10,4")
case .iPhone8Plus: self = PreviewDevice(rawValue: "iPhone10,5")
case .iPhoneX: self = PreviewDevice(rawValue: "iPhone10,6")
case .iPhoneXs: self = PreviewDevice(rawValue: "iPhone11,2")
case .iPhoneXsMax: self = PreviewDevice(rawValue: "iPhone11,6")
case .iPhoneXʀ: self = PreviewDevice(rawValue: "iPhone11,8")
case .iPhone11: self = PreviewDevice(rawValue: "iPhone12,2")
case .iPhone11Pro: self = PreviewDevice(rawValue: "iPhone12,3")
case .iPhone11ProMax: self = PreviewDevice(rawValue: "iPhone12,5")
case .iPhoneSE2: self = PreviewDevice(rawValue: "iPhone12,8")
case .iPhone12mini: self = PreviewDevice(rawValue: "iPhone13,1")
case .iPhone12: self = PreviewDevice(rawValue: "iPhone13,2")
case .iPhone12Pro: self = PreviewDevice(rawValue: "iPhone13,3")
case .iPhone12ProMax: self = PreviewDevice(rawValue: "iPhone13,4")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment