を参照してくださいタスクを検査した場合
urlString = "http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol IN ('APL')"
//let urlNSS : NSString = urlString as NSString
let urlStr : String = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let url : URL = URL(string: urlStr as String)!
let request = URLRequest(url: url)
let session = URLSession.shared
let config = URLSessionConfiguration.default
let task = session.dataTask(with: request, completionHandler: {(data, response, error) -> Void in
print("in the task")
..
..
)}
task.resume()
あなたはタスク(task.resume())作成したを再開する必要があります。デフォルトでは、タスクはサスペンド状態です。
私は別のYahoo RSSフィードURLを持つプレイグラウンドファイルを作成しました。 https://drive.google.com/file/d/0B5nqEBSJjCriWl9UTWcxSE42Yk0/view?usp=sharing
あなたの質問にあるURLはデータを与えていません。
<error xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:lang="en-US">
<description>No definition found for Table yahoo.finance.quotes</description>
</error>
コード以下のように:
//: Playground - noun: a place where people can play
import UIKit
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
var str = "Hello, playground"
let yahooURLString = "https://feeds.finance.yahoo.com/rss/2.0/headline?s=yhoo®ion=US&lang=en-US"
let yahooRSSURL: URL = URL(string: yahooURLString)!
var request = URLRequest(url: yahooRSSURL)
request.httpMethod = "GET"
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let task = session.dataTask(with: request) {data, response, err in
print("Entered the completionHandler")
print("Response JSON:\n\(String(data: data!, encoding: String.Encoding.utf8)!)")
}
task.resume()
・ホープ、このことができます。
編集: playgorundのコンソールに表示されてprint文のスクリーンショットを添付。
これはコマンドラインプログラムですか?次に、実行ループが必要です。http://stackoverflow.com/questions/25126471/cfrunloop-in-swift-command-line-program - または遊び場?次に、これは助けてください:http://stackoverflow.com/questions/24058336/how-do-i-run-asynchronous-callbacks-in-playground。 –