-4

I needed to create a variable that stored the values that I was introducing in the class each time I instantiated it.

In this way, with the creation of an object, I introduced values to an array and I wanted all the values introduced to the array to be counted every time the class was instantiated.

Thanks to @JoakimDanielson for his answer to use static variables I got it.

This is the code:

    import UIKit

class Task{
    var tasks = [String]()
    static var tasksIntroduced:Int = 0
    static var numberInstances = 0
    static var tasksCounter:Int = 0
    
    func giveMeTasksName(){
        for value in tasks{
            print ("Calling giveMeTaskName function -> The values of the tasks are: \(value)")
        }
    }
    func giveMeNumberTasksObject(){
        print("Calling giveMeNumberTasksObject function -> The value of the numbers of tasks in object are: \(Task.tasksIntroduced)")
    }
    func giveMeNumberTotalTasks(){
        //Task.tasksCounter = Task.numberInstances * Task.tasksIntroduced
        print ("Calling giveMeNumberTotalTasks function -> The tasksCounter variable value is: \(Task.tasksCounter)")
    }
    init(tasks:[String]){
        self.tasks = tasks
        Task.tasksIntroduced =  tasks.count
        Task.tasksCounter = Task.tasksIntroduced + Task.tasksCounter
        //Task.numberInstances += 1            
    }
}
print ("--- Object 1 ---")
var taskObject1 = Task(tasks:["washing clothes", "cleaning the house"])
taskObject1.giveMeNumberTasksObject()
taskObject1.giveMeNumberTotalTasks()
print ("--- Object 2 ---")
var taskObject2 = Task(tasks: ["studying"])
taskObject2.giveMeNumberTasksObject()
taskObject2.giveMeNumberTotalTasks()
print ("--- Object 3 ---")
var taskObject3 = Task(tasks: ["watering the plants", "shopping"])
taskObject3.giveMeNumberTasksObject()
taskObject3.giveMeNumberTotalTasks()
print ("--- Object 4 ---")
var taskObject4 = Task(tasks: ["study physics", "studying maths", "studying project management"])
taskObject3.giveMeNumberTasksObject()
taskObject3.giveMeNumberTotalTasks()

And this is the result:

--- Object 1 --- Calling giveMeNumberTasksObject function -> The value of the numbers of tasks in object are: 2 Calling giveMeNumberTotalTasks function -> The tasksCounter variable value is: 2 --- Object 2 --- Calling giveMeNumberTasksObject function -> The value of the numbers of tasks in object are: 1 Calling giveMeNumberTotalTasks function -> The tasksCounter variable value is: 3 --- Object 3 --- Calling giveMeNumberTasksObject function -> The value of the numbers of tasks in object are: 2 Calling giveMeNumberTotalTasks function -> The tasksCounter variable value is: 5 --- Object 4 --- Calling giveMeNumberTasksObject function -> The value of the numbers of tasks in object are: 3 Calling giveMeNumberTotalTasks function -> The tasksCounter variable value is: 8

Blockquote

Domhuggur
  • 11
  • 3
  • 1
    So what is your question / problem? – timbre timbre Sep 04 '22 at 17:28
  • This is what you need a static variable for, see for example https://stackoverflow.com/questions/37701187/when-to-use-static-constant-and-variable-in-swif. And why are you shouting? – Joakim Danielson Sep 04 '22 at 17:31
  • Thanks u very much @JoakimDanielson. The statics variables solved my problem. I was no shouting, only emphasising the most important point :) – Domhuggur Sep 06 '22 at 12:15
  • I'm going to change the question with the correct code which works for the people who have the same question as mine. – Domhuggur Sep 06 '22 at 12:17

1 Answers1

1

Create a static variable in your class (As suggested in – Joakim Danielson's comment). Let's call it instanceCount. In your designated initializer for the class, increment the static variable. All of your other initializers should call the designated initializer, so every time you create a new instance, your static variable should be incremented.

I didn't quite follow your description of the array, and the fact that your code uses Spanish strings and variable names makes it a little harder for me to follow.

Duncan C
  • 128,072
  • 22
  • 173
  • 272