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

SOLVED: Day 19 need some help :p

Forums > 100 Days of SwiftUI

Hello everyone. I just start learning SwiftUI. Today's challenge I make a Temperature Conversion.

I think it work not so good.so there have two question for me.

First is when I use @FocusState with NavigationView, the Xcode's preview dont work but I can build succeful.I dont know just I have this problem? my Xcode Version is Version 13.2.1 (13C100).macOS is 12.1

Second is when I used FocusState but sometime the keyboard did not come out. I can just see "Done" button at right side on the screen without keyboard.

Here is my code, can someone help me? thx

//
//  ContentView.swift
//  Temperature Conversion
//
//  Created by zsjng on 2022/1/21.
//Celsius, Fahrenheit, or Kelvin

import SwiftUI

struct ContentView: View {

    @State private var inputNumber:Double = 0
    @State private var inputUnit:String = "°C"
    @State private var outputUnit:String = "F"
    @FocusState private var isOnFocus:Bool

    var outputNumber:Double {
        var answer = 0.0
        if inputUnit == "°C" && outputUnit == "F"{
            answer = inputNumber * 1.8 + 32.0
        }else if inputUnit == "F" && outputUnit == "°C"{
            answer = (inputNumber - 32.0) / 1.8
        }else if inputUnit == "°C" && outputUnit == "K"{
            answer = inputNumber + 273.15
        }else if inputUnit == "K" && outputUnit == "°C"{
            answer = inputNumber - 273.15
        }else if inputUnit == "F" && outputUnit == "K"{
            answer = (inputNumber + 459.67) * 5.0 / 9.0
        }else if inputUnit == "K" && outputUnit == "F"{
            answer = inputNumber * 1.8 - 459.67
        }else{
            answer = inputNumber
        }
        return answer
    }

    let Units:[String] = ["°C","F","K"]

    var body: some View {
        NavigationView{
            Form{
                Section{
                    TextField("Please input", value: $inputNumber, format:.number)
                        .keyboardType(.decimalPad)
                        .focused($isOnFocus)
                }header: {
                    Text("Please input there")
                }
                Picker("Input Unit",selection:$inputUnit){
                    ForEach(Units,id:\.self){
                        Text($0)
                    }
                }
                Picker("Output Unit",selection:$outputUnit){
                    ForEach(Units,id:\.self){
                        Text($0)
                    }
                }
                Text("\(inputNumber.formatted())" + "\(inputUnit) = " + "\(outputNumber.formatted())" + "\(outputUnit)")

            }.navigationTitle("Temperature Change")
                .toolbar{
                    ToolbarItemGroup(placement: .keyboard){
                        Spacer()
                        Button("Done"){
                            isOnFocus = false
                        }
                    }
                }
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

2      

Dealing with your second question first. It is likely that you are toggling the Connect Hardware Keyboard option in the simulator. Press ⇧ + ⌘ + K to toggle the setting in the simulator. Alternatively under the I/O menu, in the keyboard sub-menu toggle the Connect Hardware Keyboard option.

Edit - To be clear when you have the hardware keyboard connected to your simulator, the decimal pad will not appear on the simulator, so you will only see the focused Done button. When you toggle the option to disconnect the hardware keyboard from the simulator, then decimal pad will now appear in the simulator, together with your 'Done' button.

As for the preview, I should not worry about it, as long as you can build and run the app on the simulator or device, that should be fine.

3      

@osmc94. These weird bugs are hard for new developers. Did you make the error, if so WHERE?

I pasted your entire code into a new XCode (13.2.1) project, compiled it, and ran it on my iPhone 11 with iOS 15.2.1.

It runs fine! No issues with keyboard. Navigation works nicely. Calculations are accurate.

In his videos, @twostraws has often mentioned that the Preview panel often does not work properly. Or takes its time. Or fails to draw. There are too many to note here. It may not be worth the effort to try and find a reason. Instead, keep up with the latest updates on your OS and XCode versions.

If the preview doesn't compile, try the usual: Clean Build. Test on real iPhone.

Probably not the best answer for you. Keep coding!

3      

I've given up on using the Preivew panel. It seems so flaky and inconsistent.

2      

Thanks a lot guys.I decide to reinstall my mac and Xcode .then I will see if it will solved.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.