Callback in swift 3 Playground [duplicate]

2019-02-20 02:21发布

This question already has an answer here:

Hi i am trying to execute this line of code in playground but getting any output of response.My Code is as follow:

func testCallbackEmpty( callback: @escaping  () -> Void) {

        DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
            callback()
        }
    }

    testCallbackEmpty(callback: { () -> Void in
        print("Hey called here")
    })


enum Result {
    case OK, FAILED
}


func mainCallback(callback: @escaping (Result) -> Void) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
        callback(Result.OK)
    }

}

mainCallback(callback: { result in
    print("Hurray \(result)")
})

1条回答
forever°为你锁心
2楼-- · 2019-02-20 02:43

I had to write

import UIKit

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

at the top of file

This solved.

查看更多
登录 后发表回答