xcode - 如何在 swift 中将闭包作为参数传递(只是基础知识)

标签 xcode swift

所以,我昨天才开始学习 swift,所以请耐心等待。 我正在研究关闭。我有一组非常简单的陈述。

<小时/>
    let myClosure = {
        println("this is a simple test")
    }

    func showWork( closure : ()->() ) {
      closure()
    }

showWork(myClosure)
<小时/>

我知道我做错了什么,因为 println 在 xCode Playground 中不起作用。基本上,我创建了一个简单的闭包并将其传递给我的函数。但是, println 不打印。我做错了什么?

最佳答案

Void -> Void 代替 () -> ()。所以整个事情看起来像这样:

let myClosure = {
    print("this is a simple test")
}

func showWork( closure : Void->Void ) {
    closure()
}

showWork(myClosure)

关于xcode - 如何在 swift 中将闭包作为参数传递(只是基础知识),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28588644/

相关文章:

android - 应用程序的不同名称(iOS 和 Android)

ios - 当检测到的图像不再被捕获时停止播放视频

ios - 用于 UITest 的简单 XCTest 总是失败?

ios - UICollectionView 项目间距无法调整

swift - 将 UIButton 的 CenterYAnchor 对齐到 UIPageControl Swift

ios - 在 `UIImageView` (Swift) 中加载网站预览

ios - Xcode iPhone "digest is missing"

iphone - iPhone 5 上的图像遍布整个屏幕

swift - 如何在 Swift 中添加背景音乐

ios - 表格 View 中是否可能只显示有限的行