2016-02-27 168 views
2

我有這樣的錯誤swift錯誤:代碼= 3840「結束垃圾」。

Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.}

這裏是我的SWIFT代碼:

var exercise:String = "" 

    for value in numberOfExercisesArray{ 

     exercise = exercise + value.text! + "," 

    } 
    if exercise.characters.last == ","{ 

     exercise.removeAtIndex(exercise.endIndex.predecessor()) 

    } 

我要發佈(運動)的字符串是在這種情況下:「臥推,5100」 不過我不能讓這串去,因爲上面 錯誤的,通過下面是我如何連接到我的PHP/SQL服務器:

let myURL = NSURL(string: "http://localhost:8888/saveWorkout.php"); 
     let request = NSMutableURLRequest(URL:myURL!); 
     request.HTTPMethod = "POST"; 



     let postString = "exercise=\(exercise)"; 

     request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding); 

     let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ 
      data, response, error in 

      if error != nil { 
       print("error=\(error)") 
       return 
      } 

      do{ 

       let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as? NSDictionary 

       if let parseJSON = json{ 
        let resultValue = parseJSON["status"] as! String!; 

        print("result: \(resultValue)") 

        if(resultValue == "Success"){ 



         print("worked") 


        } 
        else{ 

         print("not worked") 
        } 
       } 

      } 
      catch { print(error) } 

     } 


     task.resume(); 

我該如何解決這個錯誤 謝謝

回答

1

你的問題是,你正在使用Json,而當你試圖解析json實際上你並沒有完全解析它時,Json可以解析爲Dictionary或Array,檢查你的json響應嘗試將它在任何在線JSON解析器解碼,看看它是如何模樣,

  • 複製你的服務器響應(JSON)在谷歌
  • 搜索(在線JSON格式)
  • 過去,那裏看看