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

How to display number as output?

Forums > SwiftUI

@onqun  

Hey, I am trying to build a counter app. I am unable to display number. Counter works fine. Can someone explain to me what am I missing here

Content View

@ObservedObject var item = Item(itemName: "" , itemCount: 0)

        VStack{
            TextField("Write Name", text:$item.itemName)
        VStack{
            HStack {
                VStack{
                    Button ( action:{
                        item.add()
                        print("\(item.itemCount)")
                    }, label: {
                        Image(systemName: "arrowtriangle.up.square.fill")
                            .frame(width: 19.0, height: 19.0)
                            .imageScale(.large)
                            .foregroundColor(.accentColor)
                    })

                    Button ( action:{
                        item.sub()
                        print("\(item.itemCount)")
                    }, label: {
                        Image(systemName: "arrowtriangle.down.square.fill")
                            .frame(width: 19.0, height: 19.0)
                            .imageScale(.large)
                            .foregroundColor(.accentColor)
                    })

                }
                Text("\(item.itemCount)")

I created a model for my item

import SwiftUI

class Item: ObservableObject  {

   @Published var itemName: String
   @Published var itemCount: Int

    init(itemName: String? = nil, itemCount: Int) {
        self.itemName = itemName ?? ""
        self.itemCount = itemCount
    }

    func add() {
        itemCount += 1
    }

    func sub() {
        itemCount -= 1
    }
}

2      

Can you show your entire ContentView?

3      

@onqun  

//
//  ContentView.swift
//  betterCounter
//
//  Created by Ongun Palaoğlu on 29.07.2023.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        @ObservedObject var item = Item(itemName: "" , itemCount: 0)

        VStack{
            TextField("Write Name", text:$item.itemName)
        VStack{
            HStack {
                VStack{
                    Button ( action:{
                        item.add()
                        print("\(item.itemCount)")
                    }, label: {
                        Image(systemName: "arrowtriangle.up.square.fill")
                            .frame(width: 19.0, height: 19.0)
                            .imageScale(.large)
                            .foregroundColor(.accentColor)
                    })

                    Button ( action:{
                        item.sub()
                        print("\(item.itemCount)")
                    }, label: {
                        Image(systemName: "arrowtriangle.down.square.fill")
                            .frame(width: 19.0, height: 19.0)
                            .imageScale(.large)
                            .foregroundColor(.accentColor)
                    })

                }
                Text("\(item.itemCount)")
            }
            .padding()
        }
    }
    }
}

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

2      

Move @ObservedObject var item = Item(itemName: "" , itemCount: 0) outside var body: some View {

struct ContentView: View {
    @ObservedObject var item = Item(itemName: "" , itemCount: 0)

    var body: some View {
        VStack{
            TextField("Write Name", text:$item.itemName)
            VStack{
                HStack {
                    VStack{
                        Button ( action:{
                            item.add()
                            print("\(item.itemCount)")
                        }, label: {
                            Image(systemName: "arrowtriangle.up.square.fill")
                                .frame(width: 19.0, height: 19.0)
                                .imageScale(.large)
                                .foregroundColor(.accentColor)
                        })

                        Button ( action:{
                            item.sub()
                            print("\(item.itemCount)")
                        }, label: {
                            Image(systemName: "arrowtriangle.down.square.fill")
                                .frame(width: 19.0, height: 19.0)
                                .imageScale(.large)
                                .foregroundColor(.accentColor)
                        })

                    }
                    Text("\(item.itemCount)")
                }
                .padding()
            }
        }
    }
}

2      

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!

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.