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

}
  • 3
    What do you mean by "value in array = true" and "show next code"? – Sweeper Sep 02 '17 at 11:29
  • 2
    Who upvoted this ... question?? – luk2302 Sep 02 '17 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` . –  Sep 02 '17 at 11:34
  • what do you mean by "code"? – Mo Abdul-Hameed Sep 02 '17 at 11:36
  • @MoeAbdul-Hameed any code, for example show numbers values in array in console –  Sep 02 '17 at 11:39
  • Your explanation is still hard to understand. Showing an example input and an expected result would explain what you really want to do better. Please add an example (or some examples) into your question. – OOPer Sep 02 '17 at 12:00
  • your index is of type int and you are trying to append it to an string type array how does it possible, correct your question as per your requirement. – Krishnarjun Banoth Sep 02 '17 at 12:04

4 Answers4

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
mfaani
  • 33,269
  • 19
  • 164
  • 293
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 :)

Saurabh Jain
  • 1,688
  • 14
  • 28
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)")
  }

}
Krishnarjun Banoth
  • 1,410
  • 1
  • 15
  • 30
  • 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? –  Sep 02 '17 at 12:35
  • https://stackoverflow.com/questions/45957220/how-can-i-save-an-array-to-userdefaults-and-then-retrieve-that-array-and-print/45957586#45957586 – Krishnarjun Banoth Sep 02 '17 at 12:38
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)
}
OhadM
  • 4,687
  • 1
  • 47
  • 57
  • @user, Code snip is exactly what you need, as you wrote *if "value for array exist" * - This is what the first code snip does. – OhadM Sep 02 '17 at 12:16
  • In array I save numbers of row. In `cellForRowAt` I should show text for numbers of rows in array. –  Sep 02 '17 at 12:19
  • So your problem with the code snip is the type ? - updated answer – OhadM Sep 02 '17 at 12:20