WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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()
    }
}

   

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)

   

Much appreciated!

   

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.

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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

Reply to this topic…

You need to create an account or log in to reply.

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.