2016-06-30 2 views
-1

저는 Swift 2.0 및 Xcode Beta 2를 사용하여 첫 번째 iOS 앱을 개발하고 있습니다. 외부 JSON을 읽고 데이터가있는 테이블 뷰에서 목록을 생성합니다. 오류가 신속 (CAL에서 추가 인수 오류)추가 인수 오류가 있습니다.

@IBAction func registerbutton(sender: AnyObject) { 
    func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool { 
    //declare parameter as a dictionary which contains string as key and value combination. 
    var parameters = ["name": Name.text!, "emailaddress": EmailAddress.text!, "phonenumber": PhoneNumber.text!, "Dealerloaction": dealerlocationtextfield.text!] as Dictionary<String, String> 

    //create the url with NSURL 
    let url = NSURL(string: "http://192.168.1.75:3002/users/sign_in") //change the url 

    //create the session object 
    var session = NSURLSession.sharedSession() 

    //now create the NSMutableRequest object using the url object 
    let request = NSMutableURLRequest(URL: url!) 
    request.HTTPMethod = "POST" //set http method as POST 

    var err: NSError? 
    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &err) // pass dictionary to nsdata object and set it as request body 

    request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") 
    request.addValue("application/json", forHTTPHeaderField: "Accept") 

    //create dataTask using the session object to send data to the server 
    var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in 
     print("Response: \(response)") 
     var strData = NSString(data: data!, encoding: NSUTF8StringEncoding) 
     print("Body: \(strData)") 
     var err: NSError? 
     var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary 

     // Did the JSONObjectWithData constructor return an error? If so, log the error to the console 
     if(err != nil) { 
      print(err!.localizedDescription) 
      let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding) 
      print("Error could not parse JSON: '\(jsonStr)'") 
     } 
     else { 
      // The JSONObjectWithData constructor didn't return an error. But, we should still 
      // check and make sure that json has a value using optional binding. 
      if let parseJSON = json { 
       // Okay, the parsedJSON is here, let's get the value for 'success' out of it 
       var success = parseJSON["success"] as? Int 
       println("Succes: \(success)") 
      } 
      else { 
       // Woa, okay the json object was nil, something went worng. Maybe the server isn't running? 
       let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding) 
       print("Error could not parse JSON: \(jsonStr)") 
      } 
     } 
    }) 

    task.resume() 
     return true 
} 
+0

실제 오류 메시지를 포함시켜주십시오. –

답변

1

오류가 스위프트의 처리를 사용하여 데이터 후 서버에 대해이 코드에서 오는 : 그러나, 나는 내가 해결할 수없는 것 이상한 약간의 오류를 받고 있어요 Objective-C의 오류 처리와 다릅니다. Swift에서 NSJSONSerialization.dataWithJSONObject에는 오류에 대한 매개 변수가 없습니다. 실제로 예외를 던지고 새로운 오류 처리 시스템을 준수합니다.