How do I run Asynchronous callbacks in Playground

前端 未结 8 660
夕颜
夕颜 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:07

    While you can run a run loop manually (or, for asynchronous code that doesn't require a run loop, use other waiting methods like dispatch semaphores), the "built-in" way we provide in playgrounds to wait for asynchronous work is to import the XCPlayground framework and set XCPlaygroundPage.currentPage.needsIndefiniteExecution = true. If this property has been set, when your top level playground source finishes, instead of stopping the playground there we will continue to spin the main run loop, so asynchronous code has a chance to run. We will eventually terminate the playground after a timeout which defaults to 30 seconds, but which can be configured if you open the assistant editor and show the timeline assistant; the timeout is in the lower-right.

    For example, in Swift 3 (using URLSession instead of NSURLConnection):

    import UIKit
    import PlaygroundSupport
    
    let url = URL(string: "http://stackoverflow.com")!
    
    URLSession.shared.dataTask(with: url) { data, response, error in
        guard let data = data, error == nil else {
            print(error ?? "Unknown error")
            return
        }
    
        let contents = String(data: data, encoding: .utf8)
        print(contents!)
    }.resume()
    
    PlaygroundPage.current.needsIndefiniteExecution = true
    

    Or in Swift 2:

    import UIKit
    import XCPlayground
    
    let url = NSURL(string: "http://stackoverflow.com")
    let request = NSURLRequest(URL: url!)
    
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.currentQueue()) { response, maybeData, error in
        if let data = maybeData {
            let contents = NSString(data:data, encoding:NSUTF8StringEncoding)
            println(contents)
        } else {
            println(error.localizedDescription)
        }
    }
    
    XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
    
    0 讨论(0)
  • 2020-11-22 12:07

    Swift 4, Xcode 9.0

    import Foundation
    import PlaygroundSupport
    
    PlaygroundPage.current.needsIndefiniteExecution = true
    
    let url = URL(string: "https://jsonplaceholder.typicode.com/posts/1")!
    
    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
        guard error == nil else {
            print(error?.localizedDescription ?? "")
            return
        }
    
        if let data = data, let contents = String(data: data, encoding: String.Encoding.utf8) {
            print(contents)
        }
    }
    task.resume()
    
    0 讨论(0)
提交回复
热议问题