匿名闭包不能在具有显式参数的闭包内使用

Ján*_*nos 10 closures ios swift

可以smb解释是什么问题,我应该如何修改我的代码?

我需要过滤CKRecord从中返回的s CloudKit.

override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {

    let defaultContainer = CKContainer.defaultContainer()
    let publicDatabase = defaultContainer.publicCloudDatabase

    let myfunc2 = myfunc(names, { (records: [CKRecord], error: NSError) in
        if error == nil {

            let records2 = records.filter($0.value > sourceIndexPath.row && $0.value < destinationIndexPath.row)

            let mro = CKModifyRecordsOperation(recordsToSave: [], recordIDsToDelete: [])

        } else {

        }
    })
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Air*_*ity 22

有两种方法可以编写闭包:使用显式参数名称,或者通过引用参数$ 0,$ 1等.

例如,这两件事是等价的:

// implicit argument names, $0 and $1
let x = reduce(1...5, 0) { $0 + $1 }

// explicit argument names i and j
let y = reduce(1...5, 0) { i, j in i + j }
Run Code Online (Sandbox Code Playgroud)

但你不能混合这些东西 - 要么命名参数,要么你使用$n.你不能两个都做:

// name the arguments, but still use $0 and $1
let x = reduce(1...5, 0) { $0 + $1 }
// compiler error: Anonymous closure arguments cannot be used
// inside a closure that has explicit arguments
Run Code Online (Sandbox Code Playgroud)

在您的示例中,您似乎忘记为方法提供闭包filter.这意味着你$0不在没有参数的新闭包内 - 所以Swift编译器认为你的$0是指外部闭包,它明确地将它的参数命名为recordserror.所以它抱怨你不能将参数引用为$0具有显式参数名称的闭包内部.

(修复当然是实际提供一个闭合,filter即替换你(){})