search

Home  >  Q&A  >  body text

Objective-c - Problems with request parameters in network requests in swift.

That’s it. Since the project is relatively simple, Alamofire is no longer needed on the network side. I wrote NSURLSession myself

private func requestWithParameters(method:Method = .GET, parameters:[String:AnyObject], url:String ,completeHandle:(Bool,AnyObject?,NSError?) ->Void)
{
    let url = NSURL(string: url)

    //set up request
    let request = NSMutableURLRequest(URL:url!)
    request.timeoutInterval = 15
    request.HTTPMethod = method.rawValue
    
    //set up parameters
    print("parameters:\(parameters)")
    var param = "?"
    for item in parameters {
        if item.1 is String{
            param += item.0 + "=" + (item.1 as! String) + "&"
        }else{
            param += item.0 + "=" + String(format: "%i",item.1 as! Int) + "&"
        }
    }
    

Half of the code in the request is as above because one of the parameters is of type Int. Suddenly I don’t know how to splice the request parameters. I can’t turn my head around. The above writing is wrong. Please help me. Write. . .

迷茫迷茫2792 days ago405

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-05-02 09:24:47

    for item in parameters {
    
      param += "\(item.0)=\(item.1)&"
    }
    

    reply
    0
  • Cancelreply