2016-04-15 15 views
0

に私は、辞書サーバに送信する変数の型になりたかったが、しかし、私は問題let task = session.dataTaskWithRequest(todosUrlRequest)エラーに直面した行に:Cannot convert value of type 'NSURL' to expected argument type 'NSURLRequest'エラーが迅速

私は2つの質問

を持っていました1)このエラーは何ですか?

2)POSTに使用した手順は正しいですか?何も必要ありません。 ?? は、ライン2番目の質問についてlet task = session.dataTaskWithRequest(todosUrlRequest)

、全く分からないのヘルプ

func data_request(){ 

      let url = "http://sample.com/api/Flight/GetTicketInformation" 
      guard let todosUrlRequest = NSURL(string: url) else { 
       print("Error: cannot create URL") 
       return 
      } 

      let request = NSMutableURLRequest(URL: todosUrlRequest) 
      request.HTTPMethod = "POST" 
      request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData 

      let newTodo = ["Roundtrip": roundTrip, 
          "OneWay": oneWay, 
          "MultiWay": multiWay, 
          "Adult": numberAdults, 
          "Child": numberchild, 
          "Baby": numberinfant, 
          "SourceCityId": cityIDOrigin, 
          "DestinationCityId": cityIDPurpose, 
          "DepartingDate": raftDate, 
          "ReturningDate": bargashtDate ] 
      let jsonTodo: NSData 
      do { 
       jsonTodo = try NSJSONSerialization.dataWithJSONObject(newTodo, options: []) 
       request.HTTPBody = jsonTodo 

      } catch { 
       print("Error: cannot create JSON from todo") 
       return 
      } 
      request.HTTPBody = jsonTodo 

      let config = NSURLSessionConfiguration.defaultSessionConfiguration() 
      let session = NSURLSession(configuration: config) 

      let task = session.dataTaskWithRequest(todosUrlRequest) { 
       (data, response, error) in 

       guard let responseData = data else { 
        print("Error: did not receive data") 
        return 
       } 
       guard error == nil else { 
        print("error calling POST on /todos/1") 
        print(error) 
        return 
       } 

       // parse the result as JSON, since that's what the API provides 
       do { 
        guard let receivedTodo = try NSJSONSerialization.JSONObjectWithData(responseData, 
                         options: []) as? [String: AnyObject] else { 
                          print("Could not get JSON from responseData as dictionary") 
                          return 
        } 
        print("The todo is: " + receivedTodo.description) 


       } catch { 
        print("error parsing response from POST on /todos") 
        return 
       } 
      } 
      task.resume() 
     } 

答えて

2

requestの代わりtodosUrlRequestしていただきありがとうございます。申し訳ありません

+0

ありがとう – Shadow