2016-10-02 7 views
0

私はSwiftとiOSの開発には新人ですが、MySQLデータベースに保存されているデータをダウンロードして解析しようとしています。「Domain = NSCocoaErrorDomain Code = 3840」というエラーが表示されるのはなぜですか? UserInfo = {NSDebugDescription =値なし} '?

私はエラーを取得しておく

Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}

私は以下の私のコードを掲載しているが、私は問題はparseJSON機能であると思いますが、代わりに、私は印刷するときなど、データの実際のダウンロードではありません'data'は '<>'を返します。ここで

は私のコードです:あなたのコード内

//properties 

weak var delegate: HomeModelProtocal! 

var data : NSMutableData = NSMutableData() 

let urlPath: String = "http://localhost/service.php" //this will be changed to the path where service.php lives 

// Function to download the incoming JSON data 
func downloadItems(){ 
    let url: URL = URL(string: urlPath)! 
    var session: URLSession! 
    let configuration = URLSessionConfiguration.default 


    session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil) 

    let task = session.dataTask(with: url) 

    task.resume() 
} 

func urlSession(_ session: URLSession, task: URLSessionDataTask, didCompleteWithError error: Error?) { 
    self.data.append(data as Data) 
} 

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { 
    if error != nil{ 
     print("Failed to download data") 
    }else{ 
     print("Data downloaded") 
     print(data) 
     self.parseJSON() 
    } 
} 

func parseJSON(){ 

    var jsonResult: NSMutableArray = NSMutableArray() 

    do{ 
     jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options: []) as! NSMutableArray 
    } catch let error as NSError { 
     print("**** sake its happened again \(error)") 
    } 

    var jsonElement: NSDictionary = NSDictionary() 
    let locations: NSMutableArray = NSMutableArray() 

    for i in 0 ..< jsonResult.count{ 
     jsonElement = jsonResult[i] as! NSDictionary 

     let location = LocationModel() 

     //the following insures none of the JsonElement values are nil through optional binding 
     if let exerciseName = jsonElement["stationName"] as? String, 
      let bodyPart = jsonElement["buildYear"] as? String 
     { 
      print(exerciseName, bodyPart) 
      location.exerciseName = exerciseName 
      location.bodyPart = bodyPart 

     } 

     locations.add(location) 

    } 

    DispatchQueue.main.async(execute: {() -> Void in 

     self.delegate.itemsDownloaded(items:locations) 

    }) 
} 
+0

はおそらく、応答が空である。このようなすべてのものは固定して


、あなたはこのような何かを得ることができますか?あなたの 'print(data)'は何を表示していますか?何かが本当にそこ –

+0

は郵便配達にそれを行うか、どこか、チェックしてみてください、それは私が使用していたURLを表示すると 私はこれが返さ取得><返します 「[ を{ "Exercise_Id": "1"、 "Exercise_Name": "Barbell Curl"、 "Body_Part": "Arms" }、 ' –

+0

私が試してみました印刷データである場合 – MHDev

答えて

1

特に悪いこと:

//This method is not being called... 
func urlSession(_ session: URLSession, task: URLSessionDataTask, didCompleteWithError error: Error?) { 
    self.data.append(data as Data) //<-This line adding self.data to self.data 
} 

その2番目のパラメータとしてURLSessionDataTaskを取る何urlSession(_:task:didCompleteWithError:)方法はありません。したがって、このメソッドは決して呼び出されません。

そしてメソッド内で、self.dataはそのメソッドが呼び出された場合でも、self.dataはまだ空に...

あなたが代わりにこのメソッドを実装する必要がありますされ、self.dataに追加されます。

func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { 
    self.data.append(data) 
} 

しかし、受信したデータを蓄積する以外に何もしたくない場合は、代理人を使う必要はありません。

そして、あなたはあなたのparseJSON()方法で強制的にキャスティングを使用している:

jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options: []) as! NSMutableArray 

.mutableContainersオプションを指定せずに。これもあなたのアプリをクラッシュさせるでしょう。

あなたのコードでは、あまりにも多くのを使用していますNSSomethings

//properties 

weak var delegate: HomeModelProtocal! 

let urlPath: String = "http://localhost/service.php" //this will be changed to the path where service.php lives 

// Function to download the incoming JSON data 
func downloadItems() { 
    let url: URL = URL(string: urlPath)! 
    let session = URLSession.shared 

    let task = session.dataTask(with: url) {data, response, error in 
     if let error = error { 
      print("Failed to download data: \(error)") 
     } else if let data = data { 
      print("Data downloaded") 
      print(data as NSData) 
      //print(String(data: data, encoding: .utf8)) 
      self.parseJSON(data: data) 
     } else { 
      print("Something is wrong...") 
     } 
    } 

    task.resume() 
} 

func parseJSON(data: Data){ 

    do { 
     if let jsonResult = try JSONSerialization.jsonObject(with: data) as? [[String: AnyObject]] { 

      var locations: [LocationModel] = [] 

      for jsonElement in jsonResult { 
       let location = LocationModel() 

       //the following insures none of the JsonElement values are nil through optional binding 
       if let exerciseName = jsonElement["stationName"] as? String, 
        let bodyPart = jsonElement["buildYear"] as? String 
       { 
        print(exerciseName, bodyPart) 
        location.exerciseName = exerciseName 
        location.bodyPart = bodyPart 

       } 

       locations.append(location) 

       DispatchQueue.main.async { 
        self.delegate.itemsDownloaded(items: locations) 
       } 
      } 
     } else { 
      print("bad JSON") 
     } 
    } catch let error as NSError { 
     print("**** sake its happened again \(error)") 
    } 
} 
+0

この問題を解決してくれてありがとうございます。 – MHDev

関連する問題