Curious to know if anyone understands why I'm getting the following error
"Cannot preview in this file" - [appname] crashed"
Running the code on my (wirelessly) attached iPhone XR works, but the in-Xcode Preview window doesn't.
Here's the log produced by the Diagnostics option: <Well, I tried to attach the zip file created from the previews-diagnostics-##-## folder, but this text field wouldn't accept that file type. Is there a particular file within the folder that I should upload instead?>
Here's the code from the ContentView file for my (very basic) project:
import SwiftUI
struct ContentView: View {
@FocusState private var amountIsFocused: Bool
@State private var checkAmount = 0.0
@State private var numberOfPeople = 0
@State private var tipPercentage = 0
let tipPercentages = [0, 10, 15, 20, 25]
var totalPerPerson: Double {
let peopleCount = Double(numberOfPeople + 2)
let tipSelection = Double(tipPercentage)
let tipValue = tipSelection / 100 * checkAmount
let grandTotal = checkAmount + tipValue
let amountPerPerson = grandTotal / peopleCount
return amountPerPerson
}
var totalAmount: Double {
let tipSelection = Double(tipPercentage)
let tipValue = tipSelection / 100 * checkAmount
let grandTotal = checkAmount + tipValue
return grandTotal
}
var body: some View {
NavigationView {
Form {
Section {
TextField("Amount", value: $checkAmount, format: .currency(code: Locale.current.currencyCode ?? "USD"))
.keyboardType(.decimalPad)
.focused($amountIsFocused)
Picker("Number of people", selection: $numberOfPeople) {
ForEach(2 ..< 100) {
Text("\($0) people")
}
}
}
Section {
Picker("Tip percentage", selection: $tipPercentage) {
ForEach(0 ..< 101) {
Text($0, format: .percent)
}
}
} header: {
Text("Select tip percentage")
}
Section {
Text("Grand total: \(totalAmount, format: .currency(code: Locale.current.currencyCode ?? "USD"))")
}
Section {
Text(totalPerPerson, format: .currency(code: Locale.current.currencyCode ?? "USD"))
} header: {
Text("Amount per person")
}
.navigationTitle("WeSplit")
.navigationBarTitleDisplayMode(.inline)
}
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button("Done") {
amountIsFocused = false
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}