Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Arrays Swift:如何检查变量是否存在_Arrays_Swift_Null Check - Fatal编程技术网

Arrays Swift:如何检查变量是否存在

Arrays Swift:如何检查变量是否存在,arrays,swift,null-check,Arrays,Swift,Null Check,我试图检查Swift中是否存在变量(或者更确切地说是数组的特定索引) 如果我使用 if let mydata = array[1] { if array[1] != nil { 如果索引有值,我会得到一个错误,如果没有,我会得到一个崩溃 如果我使用 if let mydata = array[1] { if array[1] != nil { 我收到编译器警告和/或崩溃 本质上,我只是尝试获取命令行参数(任何文件名)并检查它们是否已包含在内。我看到的所有命令行参数示例都使用switch/

我试图检查Swift中是否存在变量(或者更确切地说是数组的特定索引)

如果我使用

if let mydata = array[1] {
if array[1] != nil {
如果索引有值,我会得到一个错误,如果没有,我会得到一个崩溃

如果我使用

if let mydata = array[1] {
if array[1] != nil {
我收到编译器警告和/或崩溃

本质上,我只是尝试获取命令行参数(任何文件名)并检查它们是否已包含在内。我看到的所有命令行参数示例都使用switch/case语句,但用于检查已知文本,而不是不同的文件名

我仍然在Xcode中获得索引超出范围的错误,如下所示:

if arguments.count > 1 {
    var input = arguments[2]
} else {
}

您可以使用
contains
方法检查数组中是否存在值

例如:

let expenses = [21.37, 55.21, 9.32, 10.18, 388.77, 11.41]
let hasBigPurchase = expenses.contains { $0 > 100 } // hasBigPurchase is a boolean saying whether the array contains the value or not.

有关详细信息,请查看其。

您可以使用
contains
方法检查数组中是否存在值

if index < myData.count {
  // safe to access 
  let x = myData[index]
}
例如:

let expenses = [21.37, 55.21, 9.32, 10.18, 388.77, 11.41]
let hasBigPurchase = expenses.contains { $0 > 100 } // hasBigPurchase is a boolean saying whether the array contains the value or not.
查看其以了解更多信息。

如果索引if index < myData.count {
  // safe to access 
  let x = myData[index]
}
//安全进入 设x=myData[index] }
如果索引
试试这个:

extension Collection where Indices.Iterator.Element == Index {

    subscript (safe index: Index) -> Generator.Element? {
        return indices.contains(index) ? self[index] : nil
    }
}
然后:

现在你甚至可以做:

textField.text = stringArray[safe: anyIndex]
这不会导致崩溃,因为textField.text可以为nil,[safe:]下标如果存在,则始终返回值;如果不存在,则始终返回nil

extension Collection where Indices.Iterator.Element == Index {

    subscript (safe index: Index) -> Generator.Element? {
        return indices.contains(index) ? self[index] : nil
    }
}
然后:

现在你甚至可以做:

textField.text = stringArray[safe: anyIndex]
这不会导致崩溃,因为textField.text可以是nil,[safe:]下标如果存在则始终返回值,如果不存在则始终返回nil, 要检查索引,请执行以下操作:

if index < array.count {
// index is exist

let data = array[index]

}
如果索引
简单地说, 要检查索引,请执行以下操作:

if index < array.count {
// index is exist

let data = array[index]

}
如果索引
您必须检查数组是否包含多个项:
如果array.count>1{let mydata=array[1]…
您必须检查数组是否包含多个项:
如果array.count>1{let mydata=array[1]…
这似乎与OP的要求无关。他问的是如何判断索引是否有效。这似乎与OP的要求无关。他问的是如何判断索引是否有效。是的,这当然是显而易见的(Doh!)方法。我很好奇你应该如何在数组之外完成它,只是为了一个普通变量。我和Swift在一起的全部时间都是为事物是否存在设置意外情况。我想你的意思是:index