###前提・実現したいこと
NSUrlを指定して、そこからテキストデータをダウンロードしたいんですが、Cannot assign to immutable expression of type 'NSData.Type'というエラーが出て困っています。
###発生している問題・エラーメッセージ
Cannot assign to immutable expression of type 'NSData.Type'
###該当のソースコード
swift
1import UIKit 2 3class ViewController: UIViewController,NSURLSessionDownloadDelegate{ 4 5 var button = UIButton() 6 var text = UITextView() 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 button.frame = CGRectMake(0, 0, 300, 100) 11 text.frame = CGRectMake(0, 0, 300, 300) 12 13 text.layer.position = CGPoint(x: self.view.frame.width/2, y: 300) 14 button.layer.position = CGPoint(x: self.view.frame.width/2, y: 100) 15 16 text.backgroundColor = UIColor.magentaColor() 17 button.backgroundColor = UIColor.cyanColor() 18 19 self.view.addSubview(text) 20 self.view.addSubview(button) 21 22 button.addTarget(self, action: #selector(self.btnpush), forControlEvents: .TouchUpInside) 23 super.viewDidLoad() 24 // 通信のコンフィグを用意. 25 let myConfig:NSURLSessionConfiguration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("backgroundSession") 26 27 // Sessionを作成する. 28 let mySession:NSURLSession = NSURLSession(configuration: myConfig, delegate: self, delegateQueue: nil) 29 30 // ダウンロード先のURLからリクエストを生成. 31 let myURL:NSURL = NSURL(string: "http://150.15.97.228/~yamaguchi/data1")! 32 let myRequest:NSURLRequest = NSURLRequest(URL: myURL) 33 34 // ダウンロードタスクを生成. 35 let myTask:NSURLSessionDownloadTask = mySession.downloadTaskWithRequest(myRequest) 36 37 // タスクを実行. 38 myTask.resume() 39 } 40 41 /* 42 ダウンロード終了時に呼び出されるデリゲート. 43 */ 44 func btnpush(){ 45 46 } 47 func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) { 48 49 do{ 50 var data = try NSData = NSData(contentsOfURL: location, options: NSDataReadingOptions.DataReadingMappedAlways, error: nil)!} 51 catch _ as NSError { 52 print("error") 53 54 } 55 } 56 57 /* 58 タスク終了時に呼び出されるデリゲート. 59 */ 60 func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) { 61 62 if error == nil { 63 print("ダウンロードが完了しました") 64 } else { 65 print("ダウンロードが失敗しました") 66 } 67 68 } 69 70 override func didReceiveMemoryWarning() { 71 super.didReceiveMemoryWarning() 72 73 } 74}
###試したこと
ネットで調べてみましたが、解決しませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
swift2です。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/18 14:51