最後にdispatchMain()を追加して明示的にexit()すればいいみたい
http://stackoverflow.com/questions/31944011/how-to-prevent-a-command-line-tool-from-exiting-before-asynchronous-operation-co
#! /usr/bin/env swift
import Foundation
var dic: Any = ["": ""]
func printJSON(_ data: Data) {
do {
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
dic = json
print(json)
} catch {
print("parse error!")
}
}
let url = URL(string: "http://date.jsontest.com/")!
let task = URLSession.shared.dataTask(with: url, completionHandler: { data, response, error in
if let jsonData = data {
printJSON(jsonData)
DispatchQueue.main.async(execute: {print("dic = ¥(dic)"); exit(EXIT_SUCCESS)})
}
})
task.resume()
print("OK")
dispatchMain()
Swift part10 [無断転載禁止]©2ch.net
■ このスレッドは過去ログ倉庫に格納されています
10デフォルトの名無しさん (ワッチョイ 5f3c-Scgb)
2017/02/20(月) 21:45:53.42ID:E/pOlJmJ0■ このスレッドは過去ログ倉庫に格納されています
