Xcodeバージョン8.2.1(8C1002)では、ここでlet result = "4247577657"
を取得するのはなぜですか?私は私のコードが私のURL変数からこれをどのように引っ張ったか把握しようとしています。SwiftのURL抽出で間違った結果が発生しました
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let configuration = URLSessionConfiguration.default
configuration.allowsCellularAccess = true
let session = URLSession(configuration: configuration)
let url = NSURL(string: "http://kvisionphoto.com/4247577657.txt")
let task = session.dataTask(with: url as! URL) {
(data, response, error) in
guard let data = data else {print (error ?? "error") ; return}
let result = try? String(contentsOf: url as! URL)
print(result ?? "nil")
PlaygroundPage.current.finishExecution()
}
task.resume()
...サーバー上のテキストです。 http://stackoverflow.com/help/how-to-ask – Pierce
なぜ 'URLSession'の補完ブロック内で' String(contentsOf:) 'を使用していますか?完了ブロック内に既にデータがあります。 – rmaddy
@rmaddyそれはほとんど私の質問です。私は正確に私が探していたものを持っています: "4247577657"そのコードで、それは私には意味がありません。 – gosborne3