0

I'm using Xcode 11 to test swiftUI but I can't manage to get my Content_Previews working. My code is similar to the introductory talk given by Apple.

Since I don't have MacOS Catalina, I'm testing this with the Simulator and with real device. I can't manage to see the DEBUG Preview with my testData.

The ContentView.swift looks like:

import SwiftUI

struct ContentView: View {
    var devices: [Device] = []

    var body: some View {
            NavigationView {
                List(devices) { device in
                    Image(systemName: "gamecontroller")
                    VStack() {
                        Text(device.name)
                        if(device.isConnected){
                            Text("Connected").font(.subheadline).foregroundColor(.secondary)
                        }else{
                            Text("Not Connected").font(.subheadline).foregroundColor(.secondary)
                        }
                    }
                }
                .navigationBarTitle(Text("Devices"))
            }
  }
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(devices: testData)
    }
}
#endif

Then, the Device struct is the following:

import SwiftUI

struct Device : Identifiable {
    var id = UUID()
    var name: String
    var isConnected: Bool = false
}


let testData = [
    Device(name: "devA"),
    Device(name: "devB"),
    Device(name: "devC")
]

Somehow, the ContentView_Previews is not executed, even though, DEBUG is on. (I tested this putting a conditional Text View). Any idea of what is going wrong? My guess is that the PreviewProvider ContentView_Previews is not being called and that I'm missing some configuration.

Carlos Vega
  • 1,341
  • 2
  • 13
  • 35
  • 1
    You have two choices for using Previews: (1) Catalina or (2) Playgrounds. I don't have a need for previews (I have a test iPad) but I'm sure a quick search on your favorite search engine will yield good results. `ContentView_Previews` are *only* used in Xcode Preview - they aren't meant for the Simulator. –  Jul 24 '19 at 18:15
  • 1
    Here's a link (hopefully not too old) called *SwiftUI on Mojave in iOS Playgrounds*: https://ericasadun.com/2019/06/06/good-things-swiftui-on-mojave-in-ios-playgrounds/ –  Jul 24 '19 at 19:15

1 Answers1

1

Xcode only supports SwiftUI previews on macOS Catalina (10.15) or later. It can compile your PreviewProvider-conforming types on Mojave, but it will not render them.

There are two ways to render a SwiftUI view on Mojave: in a simulator (as you have discovered), or in a playground.

Here is an example iOS playground that works in Xcode 11 beta 4 on macOS Mojave 10.14.6:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Text(verbatim: "Hello \(ProcessInfo.processInfo.operatingSystemVersionString)")
                .lineLimit(nil)
                .padding()
                .background(Color.pink.opacity(0.2).cornerRadius(12))
                .border(Color.red, width: 2, cornerRadius: 12)
        }
    }
}

import PlaygroundSupport

let host = UIHostingController(rootView: ContentView())
host.preferredContentSize = .init(width: 200, height: 200)
PlaygroundPage.current.liveView = host

Result:

xcode screen shot

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Catalina it's clearly needed. I though that even though I wouldn't be able to use previews I could at least run the test data on the simulator. – Carlos Vega Jul 25 '19 at 09:01