Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios swift:从数组中添加值_Ios_Swift - Fatal编程技术网

Ios swift:从数组中添加值

Ios swift:从数组中添加值,ios,swift,Ios,Swift,我想保存数组中的值,并检查数组中的值是否存在 我使用以下代码: 保值 var index = 0 var array = [Int]() self.array.append(self.index) 但我如何检查数组中的值是否存在,并在控制台中显示数组中的数字值呢 例如: 校验值 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

我想保存数组中的值,并检查数组中的值是否存在

我使用以下代码:

保值

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

self.array.append(self.index)
但我如何检查数组中的值是否存在,并在控制台中显示数组中的数字值呢

例如:

校验值

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 
    }

}
在数组中保存值

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

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

}
如何查看阵列中的所有内容

array.forEach({print($0)})
你也可以这样做:

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

根据您的问题,您希望查找数组中是否存在值:

例如:

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

for obj in array {
 if newArray.contains(obj) {
    print(newArray)
}else {
    newArray.append(obj)
}
}
let数组=[“一”、“二”、“三”、“一”]
var newArray:Array=[]
用于阵列中的obj{
如果newArray.contains(obj){
打印(新阵列)
}否则{
newArray.append(obj)
}
}

希望这对您有所帮助:)

只需更改cellForRowAt tableview委托方法,如下所示

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)")
  }

}

快捷方式非常简单,使用它您可以验证您的值,如果它存在,意味着索引(其中:)返回了您需要的索引,请将其删除

代码剪报:

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

你所说的“数组中的值=true”和“显示下一个代码”是什么意思。。。问题???@Sweeper我想在数组中增加值。检查数组中的值是否存在。如果数组中存在值,我应该显示:
//我的下一个代码
。你说的“代码”是什么意思?@moabdul Hameed任何代码,例如,在consoleUpdate中显示数组中的数字值question@user使用contain方法检查值是否正确exist@user,代码剪贴正是您所需要的,正如您所写*如果“数组的值存在”*-这是第一个代码剪切所做的。在数组中,我保存行数。在
cellForRowAt
中,我应该显示数组中行数的文本。因此,代码剪切的问题是类型?-最新答案:这是有效的。但是我可以使用User.defaults在数组中保存值吗?我需要在返回前一个控制器和返回此控制器后在单元格中显示文本。如果我回到上一个控制器,我的数组是空的。我使用导航栏在TableViewController之间移动。还是有更好的方法来解决这个问题?
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)")
  }

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