Home > Net >  SwiftUI: How can I display output of a function in content view
SwiftUI: How can I display output of a function in content view

Time:10-13

I have a function that calculates 2 values from 4 user inputs. and I want to display them. I added my outputs as @State variables.

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
    @State private var temelBP: Double = 0
    @State private var klinikBP: Double = 0
    
    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
        return (temelBP, klinikBP)
    }
    
    
    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())
                        }
                    }
                }
                .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()
                    print($klinikBP)
                }
                .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")")
                     }
                 }
             }
         }
    }
}

function works and also the button.Added a line of code in my function print(temelBP) and it prints when I click on my function. How can I display the values in the last section of my contentView, Texts.

CodePudding user response:

Set value to klinikBP var.

Button("Hesapla"){
    let hesapla = puanHesapla()
    klinikBP = hesapla.klinikBP
    temelBP = hesapla.temelBP
    print(klinikBP)
    print(temelBP)
}

Note: Not needed $ sign for printing value.

CodePudding user response:

You can set the output variables directly in your function

temelBP = 0.7 * T   0.3 * K
klinikBP = 0.5 * T   0.5 * K

Since you are no longer returning anything from the function you need to change its signature to not return anything

func puanHesapla() {...
  • Related