【问题标题】:Swift Closure Error快速关闭错误
【发布时间】:2015-09-13 03:43:31
【问题描述】:

我对 swift 和闭包语法比较陌生

运行良好:

fileUpdater.run({ path in println("\(path)") })

但我无法让它工作:

    fileUpdater.run({ (path: String) -> () in {
        for file in self.changedFiles {

        }
        self.changedFiles.removeAll(keepCapacity: true)
        println("mounted \(path)")
    }})

因错误而失败

无法使用 ((String) -> ()) 类型的参数列表调用 run

这里是函数定义:

func run(function: (path: String) -> ()) { // more code

更新:以下内容现在通过了基本检查,但随后导致 LLVM 失败。

fileUpdater.run({ path in {
    return println("mounted \(path)")
}})

多么可怕的错误信息:

Global is external, but doesn't have external or weak linkage!
i8* ()*    @_TFFFC10SwiftTest211AppDelegate11updateFilesFS0_FT_T_U0_FSST_auL_4pathSS
invalid linkage type for function declaration
i8* ()* @_TFFFC10SwiftTest211AppDelegate11updateFilesFS0_FT_T_U0_FSST_auL_4pathSS
LLVM ERROR: Broken module found, compilation aborted

如果有帮助的话,我正在使用 Xcode 6.4(所以我认为是 Swift 1.x?)。

【问题讨论】:

  • 将其定义为func run(function: (String) -> ()) 有帮助吗?我无法重现这个。
  • 不抱歉,没有帮助
  • 你能告诉我们你的run函数吗?

标签: swift closures


【解决方案1】:

移除块体周围多余的花括号。它们既不需要也不允许。

这是错误的:

fileUpdater.run({ path in { < curly brace not allowed
    println("mounted \(path)")
}})
^
curly brace not allowed

这是正确的:

fileUpdater.run({ path in
    println("mounted \(path)")
})

【讨论】:

    猜你喜欢
    • 2014-06-23
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多