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

New to swift, where do I place the operational part of my code to make this work?

Forums > SwiftUI

Below is a simplified example of what i'm trying to accomplish. I'm still learning everything I can, any help or other recommendations will be much appreciated!

Thanks,

import SwiftUI

struct ContentView: View {

    @State var userInput1 = ""
    @State var userInput2 = ""
    @State var userInput3 = ""
    @State var userInput4 = ""
    @State var userInput5 = ""
    @State var userInput6 = ""
    @State var userInput7 = ""

    @State var Result1 = ""
    @State var Result2 = ""
    @State var Result3 = ""

    @State var numberFormatter = NumberFormatter()

    @State private var resultsPopover = false

    let rows = [
        GridItem(.adaptive(minimum: 150))
    ]

    var body: some View {

        NavigationView {
            ScrollView {
                VStack {

                    Text("userInput1")
                        .font(.title3)
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .padding([.horizontal, .top])
                    TextField("Double #", value: $userInput1, formatter: numberFormatter)
                        .font(.title3)
                        .foregroundColor(.blue)
                        .padding(.horizontal)
                        .keyboardType(.numbersAndPunctuation)
                        .frame(maxWidth: .infinity, alignment: .leading)
                    Text("userInput2")
                        .font(.title3)
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .padding(.horizontal)
                    TextField("Double #", value: $userInput2, formatter: numberFormatter)
                        .font(.title3)
                        .foregroundColor(.blue)
                        .padding(.horizontal)
                        .keyboardType(.numbersAndPunctuation)
                        .frame(maxWidth: .infinity, alignment: .leading)
                    VStack {

                        Text("userInput3")
                            .font(.title3)
                            .frame(maxWidth: .infinity, alignment: .leading)
                            .padding(.horizontal)
                        TextField("Double #", value: $userInput3, formatter: numberFormatter)
                            .font(.title3)
                            .foregroundColor(.blue)
                            .padding(.horizontal)
                            .keyboardType(.numbersAndPunctuation)
                            .frame(maxWidth: .infinity, alignment: .leading)
                        Text("userInput4")
                            .font(.title3)
                            .frame(maxWidth: .infinity, alignment: .leading)
                            .padding(.horizontal)
                        TextField("Double #", value: $userInput4, formatter: numberFormatter)
                            .font(.title3)
                            .foregroundColor(.blue)
                            .padding(.horizontal)
                            .keyboardType(.numbersAndPunctuation)
                            .frame(maxWidth: .infinity, alignment: .leading)
                        Text("userInput5")
                            .font(.title3)
                            .frame(maxWidth: .infinity, alignment: .leading)
                            .padding(.horizontal)
                        TextField("Double #", value: $userInput5, formatter: numberFormatter)
                            .font(.title3)
                            .foregroundColor(.blue)
                            .padding(.horizontal)
                            .keyboardType(.numbersAndPunctuation)
                            .frame(maxWidth: .infinity, alignment: .leading)
                        Text("userInput6")
                            .font(.title3)
                            .frame(maxWidth: .infinity, alignment: .leading)
                            .padding(.horizontal)
                        TextField("Double #", value: $userInput6, formatter: numberFormatter)
                            .font(.title3)
                            .foregroundColor(.blue)
                            .padding(.horizontal)
                            .keyboardType(.numbersAndPunctuation)
                            .frame(maxWidth: .infinity, alignment: .leading)
                        Text("userInput7")
                            .font(.title3)
                            .frame(maxWidth: .infinity, alignment: .leading)
                            .padding(.horizontal)
                        TextField("Double #", value: $userInput7, formatter: numberFormatter)
                            .font(.title3)
                            .foregroundColor(.blue)
                            .padding(.horizontal)
                            .keyboardType(.numbersAndPunctuation)
                            .frame(maxWidth: .infinity, alignment: .leading)
                    }
                    Spacer()
                    Button() {

                        resultsPopover = true

                    } label: {
                        Label("Calculate", systemImage: "function")
                            .labelStyle(.titleAndIcon)
                            .padding()
                        //.background(Color(.systemFill))
                        //.clipShape(RoundedRectangle(cornerRadius: 20, style: .continuous))
                            .overlay {
                                RoundedRectangle(cornerRadius: 20, style: .continuous).stroke(.black, lineWidth: 1)
                            }
                        //.shadow(radius: 1)

                    }
                    .popover(isPresented: $resultsPopover) {
                        VStack {
                            Text("Result1")
                                .font(.title)
                                .frame(maxWidth: .infinity)
                                .padding([.bottom, .horizontal], 5)
                            Text(Result1)
                                .font(.title)
                                .foregroundColor(.blue)
                                .padding([.bottom, .horizontal], 5)
                                .frame(maxWidth: .infinity)
                            Text("Result2")
                                .font(.title)
                                .frame(maxWidth: .infinity)
                                .padding([.vertical, .horizontal], 5)
                            Text(Result2)
                                .font(.title)
                                .foregroundColor(.blue)
                                .padding([.bottom, .horizontal], 5)
                                .frame(maxWidth: .infinity)
                            Text("Result3")
                                .font(.title)
                                .frame(maxWidth: .infinity)
                                .padding([.vertical, .horizontal], 5)
                            Text(Result3)
                                .font(.title)
                                .foregroundColor(.blue)
                                .padding([.bottom, .horizontal], 5)
                                .frame(maxWidth: .infinity)
                        }//popoverVStack
                    }//.popover(isPresented:
                    .font(.title3)

                    Spacer()
                }//VStack
                .navigationTitle("Calculations")
            } //ScrollView
        } //NavigationView
    } //var body: some View

    let O = Double($userInput1)
    let A = Double($userInput2)
    let B = Double($userInput3)
    let I = Double($userInput4)
    let H = Double($userInput5)
    let W = Double($userInput6)
    let T = Double($userInput7)

    if O != nil && A != nil && B != nil && I != nil && H != nil && W != nil && T != nil {

        let r1 = ( O! + A! + H! )
        let r2 = ( B! + I! + W! )
        let r3 = ( T! )

        //Display the result
        Result1.text = "\(r1)"
        Result2.text = "\(r2)"
        Result3.text = "\(r3)"
    }else{
        Result1.text = "Error"
        Result2.text = ""
        Result3.text = ""

    }//else
}//struct

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

   

Logan seeks advice:

I'm still learning everything I can, any help or other recommendations will be much appreciated!

This entire site, top to bottom, is dedicated to helping you learn SwiftUI to write programs for iOS! @twoStraws has a great 100 day program, complete with videos, humor, homework assignments, fluffy dogs, and quizzes. Bonus: You get this free Forums section to get advice from other programmers and graduates! Extra Bonus: IT'S FREE.

Your code looks like you tried your best to guess how SwiftUI code should work. But it's evident that you're not clear on the Swifty way to accomplish your goals.

You ask for recommendations? I recommend you Step Away from the Code. Start with Day 1 and work through the 100 Days of SwiftUI. You'll start with smaller projects, learn new techniques, trash old code, and build a great foundation.

As you progress throught the 100 Days of SwiftUI lessons, you'll be able to answer your own questions. When you have problems, let us know what day you're on, and what concepts trip you up. There are plenty of graduates here willing to help.

The answer to your question about where to "place the operational part of my code" can't really be answered in a short post. This is covered in many lessons. Jumping right to day 20, or 32 isn't a quick, easy answer.

Really recommend you start with Day 1.

   

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.