Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.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
Swift 在闭包中使用隐式数组值_Swift - Fatal编程技术网

Swift 在闭包中使用隐式数组值

Swift 在闭包中使用隐式数组值,swift,Swift,我有一个StructBook的books数组,我正在循环查找它的一个属性是否等于一个特定的预定义值,然后我想重新排列元素 if books.contains(where: { $0.structProperty == value }) { books.rearrange(from: $0, to: 1) } 这是数组扩展文件中声明的重排函数 extension Array { mutating func rearrange(from: Int, to: Int) {

我有一个StructBook的books数组,我正在循环查找它的一个属性是否等于一个特定的预定义值,然后我想重新排列元素

if books.contains(where: { $0.structProperty == value }) {

    books.rearrange(from: $0, to: 1)
}
这是数组扩展文件中声明的重排函数

extension Array {
    mutating func rearrange(from: Int, to: Int) {
        insert(remove(at: from), at: to)
    }
}
使用此设置,我会遇到以下编译错误:

闭包中不包含匿名闭包参数

如何在不依赖for循环的情况下实现它?

返回一个布尔值,指示是否存在匹配项 元素是否存在于数组中,以及

{
    books.rearrange(from: $0, to: 1)
}
不是闭包–它是if语句中的代码块

你需要知道第一个的位置 匹配元素或零(如果不存在):

if let idx = books.index(where: { $0.structProperty == value }) {
    books.rearrange(from: idx, to: 1)
}
还请注意,数组的第一个索引为零,因此如果 目的是将数组元素移动到前面,然后它应该 是


谢谢,这很有效。我之所以选择它:1是因为我实际上想重新排列它,使其在UITableView中显示为第二。
books.rearrange(from: idx, to: 0)