2016-07-05 78 views
0

我在斯威夫特是新的,我不能找到解决这个代码:当我建立了它HTTP请求和条件2

func presentationlum() { 
    let request = NSMutableURLRequest(URL: NSURL(string: "http://raspberrypi.local/etatlum.php")!) 

    let Session = NSURLSession.sharedSession() 
    request.HTTPMethod = "GET" 

    var JsonDict=NSArray() 
    let dem = Session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in 
     do{ 
      JsonDict = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(rawValue: 0))as! NSArray 

     } 
     print(JsonDict) 

    }) 
    dem.resume() 


    if JsonDict[0] as! String == "0" 
    { 
     print("it works !") 
    } 
    else 
    { 
     print("it works to !") 
    } 

} 

我有一个“主题1:信号SIGABRT”错误。 服务器只发送一个Json_encode数组,只有01(例如:["1","0","0","1","0"])。 我只想得到这个回应,并对它做条件,但我不能。 请帮忙,谢谢。

回答

0

您完整的块之前访问你的答案:

func presentationlum() { 
    let request = NSMutableURLRequest(URL: NSURL(string: "http://raspberrypi.local/etatlum.php")!) 

    let Session = NSURLSession.sharedSession() 
    request.HTTPMethod = "GET" 

    var JsonDict : NSArray 
    let dem = Session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in 
     do{ 
      if let _JsonDict = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())as? NSArray 
      { 
       JsonDict = _JsonDict 
       if JsonDict[0] as! String == "0"{ 
        print("it works !") 
       }else{ 
         print("it works to !") 
       } 

      }else{ 
       print("Cannot parse JSON answer") 
      } 
     }catch { 
     print("An Error as occurred : \(error)") 
     } 
     print(JsonDict) 
    }) 
    dem.resume() 
}