Modifying a Struct Element in Array

I’m very new to xCode, so the answer to this might be easy

 

In ContentView.swift I have the following struct:

struct day {
    //var id = UUID()
    var x1: Int
    var x2: Int
    var x3: Int
    var bal: Int
}

 

Then I make an array with it:

var week = [  day(x1: -1, x2:   0, x3: 0, bal: 100),
              day(x1: -5, x2: -10, x3: 0, bal: 100),
              day(x1: -5, x2: -10, x3: 0, bal: 100),
              day(x1: -5, x2: -10, x3: 0, bal: 100),
              day(x1: -5, x2: -10, x3: 0, bal: 100),
              day(x1: -5, x2: -10, x3: 0, bal: 100),
              day(x1: -5, x2: -10, x3: 0, bal: 100)]

 

How do I modify it? This leads to error, telling me this type of code is not allowed in the top line of code:

week[1].bal = 111

Where else do I put it? My end goal is to make each bal = x1 + x2 + x3

 

 

Here’s the entire code if you need the big picture:

import SwiftUI

struct day {
    //var id = UUID()
    var x1: Int
    var x2: Int
    var x3: Int
    var bal: Int
}

var starting = 2000
var week = [  day(x1: -1, x2:   0, x3: 0, bal:  100),
              day(x1: -5, x2: -10, x3: 0, bal: 100),
              day(x1: -5, x2: -10, x3: 0, bal: 100),
              day(x1: -5, x2: -10, x3: 0, bal: 100),
              day(x1: -5, x2: -10, x3: 0, bal: 100),
              day(x1: -5, x2: -10, x3: 0, bal: 100),
              day(x1: -5, x2: -10, x3: 0, bal: 100)]

//week[1].bal = 111

struct ContentView: View {
    var body: some View {
        
        VStack{
            Text("Name of Month")
            //Text(String(arrayname[2][3]))
            
            //Text(String(week[1].x1))
            
            //---------------drwaing a month------------------
            VStack{
                ForEach(0..<1){ row in
                    //-------------------drawing a week--------------------
                    Text("WEEK #" + String(row+1))
                    HStack{
                        ForEach(0..<7){ number in
                            VStack{
                                Text(String(week[number].x1)).font(.caption)
                                Text(String(week[number].x2)).font(.caption)
                                Text(String(week[number].x3)).font(.caption)
                                Text(String(week[number].bal))
                            }
                            
                            Spacer()
                            
                        }
                        
                    
                    }
                    //Spacer()
                    
                }
            }
            
            
        }
    }



}

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

Powered by WPeMatico

You may also like...

Comments are closed.