1

I have a button that is supposed to append the value of the cell's label to an array.

@IBAction func test(sender: AnyObject) {
     var cellLabelArray = [""]
     cellLabelArray.append(cell.cellLabel.text)
}

I assumed that as the more I selected the button, the longer the array would become.

Like : cellLabelArray = ["label", "label", "label", "label"] if I pressed it four times.

What I'm getting is that it just appends the text label once, what I want is to append multiple times, because the label changes.

it returns something like this: ["label"] no matter how many times I press the button

How would I do this using Swift?

1 Answer 1

4

The problem is that with this line:

var cellLabelArray = [""]

you're emptying cellLabelArray at the start of the method.

So declare and initialize cellLabelArray elsewhere in your code, ex:

var cellLabelArray:[String] = []
@IBAction func test(sender: AnyObject) {
    cellLabelArray.append(cell.cellLabel.text)
}
Sign up to request clarification or add additional context in comments.

3 Comments

awesome thank you. I'll mark it as the answer when the time limit is up
How would you go about removing the same value? Since it is ".remove(at: Int)" a number would have to be set, tracked, and removed. Help in right direction to complete this?
Such an infuriating problem to be stuck at, but thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.