最佳答案
我刚接触迅速,所以我的代码中可能会有很多错误,但是我正在尝试实现的是向带有参数的本地主机服务器发送一个 GET
请求。因此,我试图实现它给我的函数采取两个参数 baseURL:string,params:NSDictionary
。我不确定如何将这两个组合到实际的 URLRequest 中?以下是我到目前为止所做的尝试
func sendRequest(url:String,params:NSDictionary){
let urls: NSURL! = NSURL(string:url)
var request = NSMutableURLRequest(URL:urls)
request.HTTPMethod = "GET"
var data:NSData! = NSKeyedArchiver.archivedDataWithRootObject(params)
request.HTTPBody = data
println(request)
var session = NSURLSession.sharedSession()
var task = session.dataTaskWithRequest(request, completionHandler:loadedData)
task.resume()
}
}
func loadedData(data:NSData!,response:NSURLResponse!,err:NSError!){
if(err != nil){
println(err?.description)
}else{
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
println(jsonResult)
}
}