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

SwiftUI Help for this code please

Forums > SwiftUI

@onqun  

Hello I made an app in ordfer to calculate two values. I calculate them in function and call the function when button pressed and finally display the values on Text. I completed the function Hopefully it is correct. I called with button. How can I display the numbers?

import SwiftUI

struct ContentView: View {

    @State private var dogruSayisiK = ""
    @State private var yalnisSayisiK = ""
    @State private var dogruSayisiT = ""
    @State private var yalnisSayisiT = ""
    @State private var ortalamaDK = 1
    @State private var ortalamaDT = 1
    @State private var STD: Double = 20
    @State private var isEditing = false

    let ortalamaT = [45, 50, 55, 60]
    let ortalamaK = [45, 50, 55, 60]
    //let SD = [17,18,19,20,21,22,23]

    func puanHesapla() ->(temelBP: Double, klinikBP: Double) {

        let yalnisNK = Double(yalnisSayisiK) ?? 0
        let dogruNK = Double(dogruSayisiK) ?? 0

        let yalnisNT = Double(yalnisSayisiT) ?? 0
        let dogruNT = Double(dogruSayisiT) ?? 0

        let toplamNetK = dogruNK - (yalnisNK*0.25)
        let toplamNetT = dogruNT - (yalnisNT*0.25)

        let zT = (toplamNetT - Double(ortalamaDT)) / STD
        let T = 50 + 10 * zT

        let zK = (toplamNetK - Double(ortalamaDK)) / STD
        let K = 50 + 10 * zK

        let temelBP = 0.7 * T + 0.3 * K

        let klinikBP = 0.5 * T + 0.5 * K
    }

    var body: some View {
        Form {

            Section(header: Text("Standard Dev.")) {
                HStack{
                    Text("17")
                    Slider(value: $STD, in: 17...23, step: 1, onEditingChanged: { editing in  isEditing = editing})
                    Text("20")
                }
                Text("\(STD, specifier: "%.1f")  ")
                    .frame(maxWidth: .infinity, alignment: .center)
                    .foregroundColor(isEditing ? .red : .blue)
            }

            Section( header: Text("Klinik Bilimler") ){
                VStack(alignment: .leading) {
                    HStack(spacing: 10) {
                        Text("Klinik Dogru Sayisi")
                            .foregroundColor(.blue)
                        TextField("Dogru Sayisi", text: $dogruSayisiK)
                            .keyboardType(.numberPad)
                    }
                    HStack(spacing: 10) {
                        Text("Klinik Yalnis Sayisi")
                            .foregroundColor(.red)
                        TextField("Yalnis Sayisi", text: $yalnisSayisiK)
                            .keyboardType(.numberPad)
                    }
                    Picker("Sinav Ortalamasi", selection: $ortalamaDK) {
                        ForEach(0 ..< ortalamaK.count) {
                            Text("\(self.ortalamaK[$0])%")
                                .pickerStyle(SegmentedPickerStyle())
                        }
                    }
                }
            }

            Section(header: Text("Temel Bilimler") ) {
                VStack (alignment: .leading) {
                    HStack(spacing: 10) {
                        Text("Temel Bilim Dogru Sayisi")
                            .foregroundColor(.blue)
                        TextField("Dogru Sayisi", text: $dogruSayisiT)
                            .keyboardType(.numberPad)
                    }

                    HStack(spacing: 10) {
                        Text("Temel Bilim Yalnis Sayisi")
                            .foregroundColor(.red)
                        TextField("Yalnis Sayisi", text: $yalnisSayisiT)
                            .keyboardType(.numberPad)
                    }

                    Picker("Temel Bilim Ortalama", selection: $ortalamaDT) {
                        ForEach(0 ..< ortalamaT.count) {
                            Text("\(self.ortalamaT[$0])%")
                        }
                    }
                }

            .pickerStyle(SegmentedPickerStyle())
        }

                Button("Hesapla"){
                    puanHesapla()

                }
                .background(Color.white)
                .foregroundColor(Color.black)
                .frame(width: 70, height: 70, alignment: .center)

             Section(header: Text("Sonuc")){
                 HStack{
                     VStack{
                         Text("Temel Bilim Puan")
                             .foregroundColor(.red)
                         Text("\(temelBP, specifier: "%.2f")")
                     }

                     VStack{
                         Text("Klinik Bilim Puan")
                             .foregroundColor(.red)
                         Text("\(KlinikBP, specifier: "%.2f")")
                     }
                 }
             }
         }
    }
}

3      

Put the result of your calculation in a Text somewhere in your View.

3      

@onqun  

I did put it in the end.

3      

Okay, your puanHesapla function returns a tuple of two Doubles (although you don't actually return anything). So you need to assign it to an @State var of the same type in the Button's action handler. Then you can access the tuple's members in the Text elements.

Make the following changes and it should work.

Add this to ContentView:

    @State private var result: (temelBP: Double, klinikBP: Double) = (0, 0)

Add this line at the end of the puanHesapla function to return the values:

        return (temelBP, klinikBP)

Change the Button's action handler to this:

result = puanHesapla()

And, finally, the two Text elements at the end should be:

Text("\(result.temelBP, specifier: "%.2f")")

Text("\(result.klinikBP, specifier: "%.2f")")

3      

@onqun  

Thank you very much. I appreciate. Now I can calculate my residency exam results from my ownphone :P

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.