###前提・実現したいこと
Qiitaの同期通信のためのコードをSwift3に対応させたかったのですが、
以下の行のみエラーが残っていてどうにもなりません。お助け下さい。
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
http://qiita.com/gomi_ningen/items/e081f4cc2415d6517941
###Qiitaに載っていたSwift2.xのコード
Swift2.x
1public class HttpClientImpl { 2 3 private let session: NSURLSession 4 5 public init(config: NSURLSessionConfiguration? = nil) { 6 self.session = config.map { NSURLSession(configuration: $0) } ?? NSURLSession.sharedSession() 7 } 8 9 public func execute(request: NSURLRequest) -> (NSData?, NSURLResponse?, NSError?) { 10 var d: NSData? = nil 11 var r: NSURLResponse? = nil 12 var e: NSError? = nil 13 let semaphore = dispatch_semaphore_create(0) 14 session 15 .dataTaskWithRequest(request) { (data, response, error) -> Void in 16 d = data 17 r = response 18 e = error 19 dispatch_semaphore_signal(semaphore) 20 } 21 .resume() 22 dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) 23 return (d, r, e) 24 } 25 26}
###該当のソースコード
Swift3.x
1public class HttpClientImpl { 2 3 private let session: URLSession 4 5 public init(config: URLSessionConfiguration? = nil) { 6 self.session = config.map { URLSession(configuration: $0) } ?? URLSession.shared 7 } 8 9 public func execute(request: URLRequest) -> (NSData?, URLResponse?, NSError?) { 10 var d: NSData? = nil 11 var r: URLResponse? = nil 12 var e: NSError? = nil 13 let semaphore = DispatchSemaphore(value: 0) 14 session 15 .dataTask(with: request) { (data, response, error) -> Void in 16 d = data as NSData? 17 r = response 18 e = error as NSError? 19 semaphore.signal() 20 } 21 .resume() 22 23 semaphore.wait(timeout: DispatchTime(uptimeNanoseconds: DispatchTime.distantFuture)) 24 25 return (d, r, e) 26 } 27}
###補足情報(言語/FW/ツール等のバージョンなど)
リファレンスはこのあたりかと...
DispatchSemaphore
DispatchTime
回答1件
あなたの回答
tips
プレビュー