Hi guys,
I can sure use the help.
I am making a few view changes to the Converter app as part of day 24 challenges. I am getting the above error on the closing bracket of the MeasurementFormatter init, and cannot get the app to compile.
The changes that I made so far is adding a screen with details of the measurement( the values is stored in a seperate swift file, struct and array, and declared by var info: UnitInfo), and changing the foreground colour of the result textfield.
Am I missing something somewhere(sure I am missing something very stupid) Code is below.
Paul
import SwiftUI
struct ContentView: View {
@State public var info: UnitInfo
@State private var inputUnit: Dimension = UnitTemperature.celsius
@State private var outputUnit: Dimension = UnitTemperature.fahrenheit
@State private var input = 100.0
@State private var selectedUnits = 0
@FocusState private var keyboardIsFocused: Bool
let formatter: MeasurementFormatter
init () {
formatter = MeasurementFormatter()
formatter.unitOptions = .providedUnit
formatter.unitStyle = .short
} - this is where I get - Return from initializer without initializing all stored properties
let conversions = ["Unit for Measure for Acceleration", "Unit for Measure for Planar Angle and Rotation", "Unit of Measure of Area", "Unit of Measure for Concentration of Mass", "Unit of Measure of Dispersion", "Unit of measure for duration of Time","Unit of Measure for Electric Charge", "Unit of Measure of Electric Current", "Unit of Measure for Electric Potential Difference","Unit of Measure for Electric Resistance","Unit of Measure for Energy","Unit of Measure for Frequency","Unit of Measure for Fuel Efficiency","Unit of Measure for Illuminance", "Unit of Measure for Quantities of Information","Unit of Measure of Lenght", "Measure of Mass", "Measure of Power", "Unit of Measure for Pressure", "Unit for Measure of Speed", "Unit of Measure for Temperature", "Unit of Measure for Volume"]
let unitTypes = [
[UnitAcceleration.metersPerSecondSquared, UnitAcceleration.gravity],
[UnitAngle.degrees, UnitAngle.arcMinutes, UnitAngle.arcSeconds, UnitAngle.radians, UnitAngle.gradians, UnitAngle.revolutions],
[UnitArea.squareMegameters, UnitArea.squareKilometers, UnitArea.squareCentimeters, UnitArea.squareMillimeters, UnitArea.squareMicrometers, UnitArea.squareNanometers, UnitArea.squareInches, UnitArea.squareFeet, UnitArea.squareYards, UnitArea.squareMiles, UnitArea.acres, UnitArea.ares, UnitArea.hectares],
[UnitConcentrationMass.gramsPerLiter, UnitConcentrationMass.milligramsPerDeciliter],
[UnitDispersion.partsPerMillion],
[UnitDuration.seconds, UnitDuration.minutes, UnitDuration.hours],
[UnitElectricCharge.coulombs, UnitElectricCharge.megaampereHours, UnitElectricCharge.kiloampereHours, UnitElectricCharge.ampereHours, UnitElectricCharge.milliampereHours, UnitElectricCharge.microampereHours],
[UnitElectricCurrent.megaamperes, UnitElectricCurrent.kiloamperes, UnitElectricCurrent.amperes, UnitElectricCurrent.milliamperes, UnitElectricCurrent.microamperes],
[UnitElectricPotentialDifference.megavolts, UnitElectricPotentialDifference.kilovolts, UnitElectricPotentialDifference.volts, UnitElectricPotentialDifference.millivolts, UnitElectricPotentialDifference.microvolts],
[UnitElectricResistance.megaohms, UnitElectricResistance.kiloohms, UnitElectricResistance.ohms, UnitElectricResistance.milliohms, UnitElectricResistance.microohms],
[UnitEnergy.kilojoules, UnitEnergy.joules, UnitEnergy.kilojoules, UnitEnergy.calories, UnitEnergy.kilowattHours],
[UnitFrequency.terahertz, UnitFrequency.gigahertz, UnitFrequency.megahertz, UnitFrequency.kilohertz, UnitFrequency.hertz, UnitFrequency.millihertz, UnitFrequency.microhertz, UnitFrequency.nanohertz],
[UnitFuelEfficiency.litersPer100Kilometers, UnitFuelEfficiency.milesPerGallon, UnitFuelEfficiency.milesPerImperialGallon],
[UnitIlluminance.lux],
[UnitInformationStorage.kilobits, UnitInformationStorage.megabits, UnitInformationStorage.gigabits, UnitInformationStorage.terabits, UnitInformationStorage.petabits, UnitInformationStorage.exabits, UnitInformationStorage.zettabits, UnitInformationStorage.yottabits, UnitInformationStorage.kibibits, UnitInformationStorage.mebibits, UnitInformationStorage.gibibits, UnitInformationStorage.tebibits, UnitInformationStorage.pebibits, UnitInformationStorage.exbibits, UnitInformationStorage.zebibits, UnitInformationStorage.yobibits ],
[UnitLength.meters, UnitLength.miles, UnitLength.feet, UnitLength.yards, UnitLength.kilometers],
[UnitMass.grams, UnitMass.kilograms, UnitMass.ounces, UnitMass.pounds, UnitMass.stones],
[UnitPower.terawatts, UnitPower.gigawatts, UnitPower.megawatts, UnitPower.kilowatts, UnitPower.watts, UnitPower.milliwatts, UnitPower.microwatts, UnitPower.nanowatts, UnitPower.picowatts, UnitPower.femtowatts, UnitPower.horsepower],
[UnitPressure.newtonsPerMetersSquared, UnitPressure.gigapascals, UnitPressure.megapascals, UnitPressure.kilopascals, UnitPressure.hectopascals, UnitPressure.inchesOfMercury, UnitPressure.bars, UnitPressure.millibars, UnitPressure.millimetersOfMercury, UnitPressure.poundsForcePerSquareInch],
[UnitSpeed.metersPerSecond, UnitSpeed.kilometersPerHour, UnitSpeed.milesPerHour, UnitSpeed.knots],
[UnitTemperature.celsius, UnitTemperature.fahrenheit, UnitTemperature.kelvin],
[UnitVolume.megaliters, UnitVolume.kiloliters, UnitVolume.liters, UnitVolume.deciliters, UnitVolume.centiliters, UnitVolume.milliliters, UnitVolume.cubicKilometers, UnitVolume.cubicMeters, UnitVolume.cubicDecimeters, UnitVolume.cubicMillimeters, UnitVolume.cubicInches, UnitVolume.cubicFeet, UnitVolume.cubicYards, UnitVolume.cubicMiles, UnitVolume.acreFeet, UnitVolume.bushels, UnitVolume.teaspoons, UnitVolume.tablespoons, UnitVolume.fluidOunces, UnitVolume.cups, UnitVolume.pints, UnitVolume.quarts, UnitVolume.gallons, UnitVolume.imperialTeaspoons, UnitVolume.imperialTablespoons, UnitVolume.imperialFluidOunces, UnitVolume.imperialPints, UnitVolume.imperialQuarts, UnitVolume.imperialGallons,UnitVolume.metricCups]
]
var result: String {
let inputMeasurement = Measurement(value: input, unit: inputUnit)
let outputMeasurement = inputMeasurement.converted(to: outputUnit)
return formatter.string(from: outputMeasurement)
}
var body: some View {
NavigationView {
Form {
Section{
TextField("Amount", value: $input, format: .number)
.keyboardType(.decimalPad)
.focused($keyboardIsFocused)
} header: {
Text("Amount to convert")
}
Picker("Conversion", selection: $selectedUnits){
ForEach(0..<conversions.count) {
Text(conversions[$0])
}
}
Picker("Convert From", selection: $inputUnit) {
ForEach(unitTypes[selectedUnits], id: \.self) {
Text(formatter.string(from: $0).capitalized)
}
}
Picker("Convert To", selection: $outputUnit) {
ForEach(unitTypes[selectedUnits], id: \.self) {
Text(formatter.string(from: $0).capitalized)
}
}
Section {
Text(result)
.foregroundColor(.red)
} header: {
Text("Results")
}
Section {
TextField("About", text: ($info.detail))
} header: {
Text("About unit of")
}
}
.navigationTitle("Converter")
.toolbar {
ToolbarItemGroup(placement: .keyboard) {
Spacer()
Button("Done") {
keyboardIsFocused = false
}
}
}
.onChange(of: selectedUnits) { newSelection in
let units = unitTypes[newSelection]
inputUnit = units[0]
outputUnit = units[1]
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}