UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 19 Challenge: Swift Compiler Error

Forums > 100 Days of SwiftUI

So I going along merrily attempting to write the code for Day 19 Challenge and then I get a Swift compiler error as follows:

"Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project"

The expression is: var body: some View {

So basically I'm dead in the water. There are no other errors that pop up but the project will not build because of the compiler error.

What can I do so I can continue coding and testing the project?

My code so far...

import SwiftUI

struct ContentView: View {
    @State private var inputValue = 0.0
    @State private var inputUnit = ""
    @State private var outputUnit = ""
    var outputValue = 0.0
    var baseUnitValue = 0.0
    var units = ["meters","kilometers", "feet", "yard", "miles"]
    @FocusState private var inputFieldIsFocused: Bool

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker("Starting Unit", selection: $inputUnit) {
                        ForEach(units, id: \.self) {
                            Text($0, format: .number)
                        }
                    }
                    .pickerStyle(.segmented)
                }
                Section {
                    Picker("Ending Unit", selection: $outputUnit) {
                        ForEach(units, id: \.self) {
                            Text($0, format: .number)
                        }
                    }
                }
                    .pickerStyle(.segmented)
                Section {
                    TextField("Enter the value to convert", value: $inputValue, format: .number)
                        .keyboardType(.decimalPad)
                        .focused($inputFieldIsFocused)
                }
                Section {
                    Text(outputValue, format: .number)
                }
            } .navigationTitle("UnitConverter")
              .navigationBarTitleDisplayMode(.inline)
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

1      

You're trying to format the values from an array of strings as numbers and Xcode doesn't like that.

var units = ["meters","kilometers", "feet", "yard", "miles"]

Section {
  Picker("Starting Unit", selection: $inputUnit) {
    ForEach(units, id: \.self) {
      Text($0, format: .number)
    }
  }
  .pickerStyle(.segmented)
}
Section {
  Picker("Ending Unit", selection: $outputUnit) {
    ForEach(units, id: \.self) {
      Text($0, format: .number)
    }
  }
}

Get rid of format: .number in both places

Picker("Starting Unit", selection: $inputUnit) {
  ForEach(units, id: \.self) {
    Text($0)
  }
}
.pickerStyle(.segmented)

1      

Much appreciated!

1      

Yep. When you get weird errors like that, try commenting out chunks of your code.

You can comment out a section of code like the following:

/*Section {
  Picker("Ending Unit", selection: $outputUnit) {
    ForEach(units, id: \.self) {
      Text($0)
    }
  }
}*/

or

//                Section {
//                    Picker("Ending Unit", selection: $outputUnit) {
//                        ForEach(units, id: \.self) {
//                            Text($0)
//                        }
//                    }
//                }

If you highlight a section of code and press command + /, it will comment out that highlighted section.

If you go through your code and comment things out a little bit at a time, or comment everything out and then uncomment things a little bit at a time, you can isolate which part of the code is producing the error.

After you make a change, you can also do command + B to verify your code builds and there are no errors without actually having to run it.

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.