How do I run Asynchronous callbacks in Playground

前端 未结 8 807
夕颜
夕颜 2020-11-22 11:38

Many Cocoa and CocoaTouch methods have completion callbacks implemented as blocks in Objective-C and Closures in Swift. However, when trying these out in Playground, the co

8条回答
  •  我在风中等你
    2020-11-22 12:00

    As of XCode 7.1, XCPSetExecutionShouldContinueIndefinitely() is deprecated. The correct way to do this now is to first request indefinite execution as a property of the current page:

    import XCPlayground
    
    XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
    

    …then indicate when execution has finished with:

    XCPlaygroundPage.currentPage.finishExecution()
    

    For example:

    import Foundation
    import XCPlayground
    
    XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
    
    NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: "http://stackoverflow.com")!) {
        result in
        print("Got result: \(result)")
        XCPlaygroundPage.currentPage.finishExecution()
    }.resume()
    

提交回复
热议问题