Search code examples
arraysswiftappendindexpath

Append Array Through If Statement


So, I am making an app that requires to select various things. You can select these by swiping to the right of the UITableView. I am using a UIContextualAction and I am using a switch statement to order out the functions and executable statements. Once the UITableView Cell is selected, it is to run off of one of my switch statements. This is from the indexPath.row chosen. Now once each object is selected, it will append the name into an array.

My problem: Once the object is appended, the array does not add on. For instance, I pick Snapchat. The array has "Snapchat" in it. If I were to also chose Instagram, the array should append "Instagram". It does, but it forgets about the "Snapchat" in it too, and when it prints, it only prints 1 selected object. (Which was the last one I picked)

How could I make an array where I can add more objects without it forgetting

I have looked at doing if statements on the tableview cell that was selected. Nothing works, and was wondering how I could fix this, or find a solution

var array = [String]()
var choice = indexPath.row
switch choice {
case 0:
    array.append("Instagram")
    print(array)
case 1:
    array.append("Snapchat")
    print(array)
case 2:
    array.append("VSCO")
    print(array)
default:
    print("Error")
    print(array)

This is what I get when I select multiple and try to append the array.

["Instagram"]
["Snapchat"]
["VSCO"]
Error

as you can see, they are not in the array form.


Solution

  • This is what you have done

    func someFunc() {
        var array = [String]()
        var choice = indexPath.row
        switch choice {
        case 0:
             array.append("Instagram")
             print(array)
        case 1:
             array.append("Snapchat")
             print(array)
        case 2:
             array.append("VSCO")
             print(array)
        default:
             print("Error")
             print(array)
    }
    

    You have declared the array you want to append to inside the function that is adding the objects to the array. What happens is everytime you add something, it overwrites the object you just entered.

    1. Function creates array
    2. Adds object to array
    3. Function ends

    4. Function creates array (overwrites old array)

    5. Adds object to array (single object only inside array now)
    6. Function ends

    Simply move your array var array = [String]() outside of the function.

     var array = [String]()
    
     override func viewDidLoad() {
          super.viewDidLoad()
     }
    
        func someFunc() {
    
            var choice = indexPath.row
            switch choice {
            case 0:
                 self.array.append("Instagram")
                 print(array)
            case 1:
                 self.array.append("Snapchat")
                 print(array)
            case 2:
                 self.array.append("VSCO")
                 print(array)
            default:
                 print("Error")
                 print(array)
        }
    

    Also, be careful when naming your array 'array', it is best practice to give the array a more meaningful name