Created
June 23, 2019 23:58
-
-
Save funzin/363ade33bf58dfea847b0a4a34d2787d to your computer and use it in GitHub Desktop.
Use previewDevice with type-safe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// SampleSwiftUI | |
// | |
// Created by 中澤 郁斗 on 2019/06/23. | |
// Copyright © 2019 中澤 郁斗. All rights reserved. | |
// | |
import SwiftUI | |
import UIKit | |
struct ContentView : View { | |
let type: DeviceType | |
init(type: DeviceType) { | |
self.type = type | |
} | |
var body: some View { | |
VStack { | |
Text(type.rawValue) | |
} | |
} | |
} | |
struct ContentView_Previews : PreviewProvider { | |
static var previews: some View { | |
let type: DeviceType = .mac | |
return ContentView(type: type) | |
.previewDevice(PVDevice(type: type).rawValue) | |
} | |
} | |
struct PVDevice { | |
let rawValue: PreviewDevice | |
init(type: DeviceType) { | |
self.rawValue = PreviewDevice(stringLiteral: type.rawValue) | |
} | |
} | |
enum DeviceType: String { | |
case mac = "Mac" | |
case iPhone7 = "iPhone 7" | |
case iPhone7Plus = "iPhone 7 Plus" | |
case iPhone8 = "iPhone 8" | |
//. | |
//. | |
//. | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment