0

I want to save value in array and check if value in array exist.

I use this code:

to save value

var index = 0
var array = [Int]()

self.array.append(self.index)

But how I can check if value in array exist and show numbers values in array in console?

example:

check value

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

    if "values for array exist" {

    //show text for all row numbers in the array 
    }

}

save value in array

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    index = indexPath.row
    self.array.append(self.index.row) //save row number

}
7
  • 3
    What do you mean by "value in array = true" and "show next code"? Commented Sep 2, 2017 at 11:29
  • 2
    Who upvoted this ... question?? Commented Sep 2, 2017 at 11:32
  • @Sweeper I want to add value in array. Check value in array exist. And if value in array exist I should show : // my next code . Commented Sep 2, 2017 at 11:34
  • what do you mean by "code"? Commented Sep 2, 2017 at 11:36
  • @MoeAbdul-Hameed any code, for example show numbers values in array in console Commented Sep 2, 2017 at 11:39

4 Answers 4

1
var index = 0
var array = [String]()

array.append("\(index)")

How to see all that is inside the array

array.forEach({print($0)})

you could also do:

for i in 0..<array.count{
    print(array[i])
}

to check if the array contains:

print(array.contains("5")) // false
print(array.contains("0")) // true
Sign up to request clarification or add additional context in comments.

Comments

0

According to your question you want to find a value exist in array or not so:

Take an example:

let array = ["one","two","three","one"]
var newArray:Array<String> = []

for obj in array {
 if newArray.contains(obj) {
    print(newArray)
}else {
    newArray.append(obj)
}
}

Hope this help you :)

2 Comments

Update question
@user use the contain method for check if value exist
0

Just change your cellForRowAt tableview delegate method as below.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

if array.contains(indexPath.row) {

       print("Rows in arrays are \(array)")
  }

}

2 Comments

It is works. But can I save value in array with User.defaults? I need to show text in cells after back in previous controller and return in this controller. And if I back in previous controller my array is empty. I use the navigation bar to move between my TableViewControllers. Or there are better way to solve the problem?
0

The Swift way is pretty easy, using index(where:) you can validate your value and if it exist, meaning, index(where:) returned the index you need, remove it.

Code snip:

if let index = array.index(where: { $0 === 1 }) {
    array.remove(at: index)
}

3 Comments

@user, Code snip is exactly what you need, as you wrote *if "value for array exist" * - This is what the first code snip does.
In array I save numbers of row. In cellForRowAt I should show text for numbers of rows in array.
So your problem with the code snip is the type ? - updated answer

Your Answer

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