コード内のリンクを使用してYahooFinanceデータセットにアクセスできます。JSONデータを実行し、デバッグウィンドウに出力します。選択したフィールドをmyNameLabelに保存して印刷するにはどうすればよいですか。たとえば、「symbol」または「LastTradePriceOnly」フィールド?
class ViewController: UIViewController {
@IBOutlet weak var myNameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let manager = AFHTTPRequestOperationManager()
manager.GET( "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22AAPL%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=",
parameters: nil,
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println("JSON: " + responseObject.description)
//How to access and print individual fields?
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println("Error: " + error.localizedDescription)
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
ネストされた辞書の構造をナビゲートできます。おそらく次のようなものです。
manager.GET( "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quote%20where%20symbol%20in%20(%22AAPL%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=",
parameters: nil,
success: {
operation, responseObject in
if let quote = responseObject?.objectForKey("query")?.objectForKey("results")?.objectForKey("quote") as? NSDictionary {
let symbol = quote.objectForKey("Symbol") as? String
let lastTradePriceOnly = quote.objectForKey("LastTradePriceOnly") as? String
println("results: \(symbol) @ \(lastTradePriceOnly)")
} else {
println("no quote")
}
},
failure: {
operation, error in
println("Error: " + error.localizedDescription)
})
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加