I am trying to create Xcode previews for my view controller in Xcode 14 beta and iOS 16. When ever I run the code, it just throws some Xcode preview error in the dialog and crashes the preview. I am not using Storyboards, so I am just loading my ViewController2 programmatically.
import Foundation
import UIKit
import SwiftUI
final class ViewController2: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.green
}
}
struct ViewController2_Previews: PreviewProvider {
static var previews: some View {
ViewController2()
}
}
extension ViewController2: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> ViewController2 {
ViewController2()
}
func updateUIViewController(_ uiViewController: ViewController2, context: Context) {
}
}
CodePudding user response:
Ouch... you use class for representable - that's bad idea (even for final!) - use only(!) structs
Here is fixed variant (tested with Xcode 14b2)
struct ViewController2_Previews: PreviewProvider {
static var previews: some View {
ViewControllerRep()
}
}
struct ViewControllerRep: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> ViewController2 {
return ViewController2()
}
func updateUIViewController(_ uiViewController: ViewController2, context: Context) {
}
}