递归函数目前会在使用 swift 的 xcode 项目中导致编译时错误,但在 Playground 中工作得很好。在 Xcode 6 beta 4 的发行说明中:
Nested functions that recursively reference themselves or other functions nested in the same outer function crash the compiler. (11266246) For example:
func foo() { func bar() { bar() } func zim() { zang() } func zang() { zim() } }
Workaround: Move recursive functions to the outer type or module context
将递归函数移动到外部类型或模块上下文是什么意思?
最佳答案
这意味着您应该在另一个函数之外声明该函数:
func bar() { bar() }
func zim() { zang() }
func zang() { zim() }
func foo() {
}
关于xcode - Swift 中递归引用自身的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25110699/