swift NSURL 更改 swift 3.1
swift NSURL changes swift 3.1
我目前正在尝试以 JSON 格式从 mySQL 检索数据 Swift。我发现 NSURL
已更改为 URL
。我正在遵循另一个旧代码来帮助指导我完成此操作,但它已经过时了。我在 let request
和 let task
处有错误 (NSURL/URL)。我需要一些帮助来弄清楚如何正确执行此操作。谢谢!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//********************************************************************
//MySQL Url Request
let URLRequest = NSURL(string: URLWarrick)
//Creating mutable request
let request = NSMutableURLRequest(url: URLRequest!)
//setting method to post
request.httpMethod = "GET"
//Task to send request
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in
//exiting if there is some error
if error != nil{
print("error is \(error)")
return;
}
do {
//converting response to NSDictionary
var teamJSON: NSDictionary!
WarrickJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
//getting the JSON array teams from the response
//let teams: NSArray = teamJSON["teams"] as! NSArray
//looping through all the json objects in the array teams
//for i in 0 ..< teams.count{
//getting the data at each index
//let teamId:Int = teams[i]["id"] as! Int!
//let teamName:String = teams[i]["name"] as! String!
//let teamMember:Int = teams[i]["member"] as! Int!
//displaying the data
//print("id -> ", teamId)
//print("name -> ", teamName)
//print("member -> ", teamMember)
//print("===================")
//print("")
}
您已经非常接近正确的格式了。为了在 Swift 3 中正确,它应该类似于以下内容:
if let let url = URL(string: URLWarrick) {
var request = URLRequest(url: url)
request.httpMethod = "GET"
var session: URLSession = URLSession.shared
let task = session.dataTask(with: request) { (data, response, error) -> Void in
// Your completion handler code goes here
}
task.resume()
}
最大的变化是,在 swift 3 中,您不再需要可变的 url 请求,它现在可以作为 var 正确处理。然后您现在使用 URLSession 发送该请求。
这是 URL 请求的典型结构。 GET
请求是默认请求,因此您不需要 NSMutableURLRequest
来指定它。
if let url = URL(string: URLWarrick) {
let request = URLRequest(url: url)
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
if let error = error {
print(error.localizedDescription)
return
}
if let data = data {
if let json = try? JSONSerialization.jsonObject(with: data) {
if let dict = json as? Dictionary<String,Any> {
if let teams = dict["teams"] as? Array<Dictionary<String,Any>> {
for team in teams {
guard
let id = team["id"] as? Int,
let name = team["name"] as? String,
let member = team["memeber"] as? Int
else {
print("Error! - Data for this team is incomplete.")
continue
}
print(id, name, member)
}
return
}
}
}
}
print("Error! - Your data was invalid.")
})
task.resume()
}
我目前正在尝试以 JSON 格式从 mySQL 检索数据 Swift。我发现 NSURL
已更改为 URL
。我正在遵循另一个旧代码来帮助指导我完成此操作,但它已经过时了。我在 let request
和 let task
处有错误 (NSURL/URL)。我需要一些帮助来弄清楚如何正确执行此操作。谢谢!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//********************************************************************
//MySQL Url Request
let URLRequest = NSURL(string: URLWarrick)
//Creating mutable request
let request = NSMutableURLRequest(url: URLRequest!)
//setting method to post
request.httpMethod = "GET"
//Task to send request
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in
//exiting if there is some error
if error != nil{
print("error is \(error)")
return;
}
do {
//converting response to NSDictionary
var teamJSON: NSDictionary!
WarrickJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
//getting the JSON array teams from the response
//let teams: NSArray = teamJSON["teams"] as! NSArray
//looping through all the json objects in the array teams
//for i in 0 ..< teams.count{
//getting the data at each index
//let teamId:Int = teams[i]["id"] as! Int!
//let teamName:String = teams[i]["name"] as! String!
//let teamMember:Int = teams[i]["member"] as! Int!
//displaying the data
//print("id -> ", teamId)
//print("name -> ", teamName)
//print("member -> ", teamMember)
//print("===================")
//print("")
}
您已经非常接近正确的格式了。为了在 Swift 3 中正确,它应该类似于以下内容:
if let let url = URL(string: URLWarrick) {
var request = URLRequest(url: url)
request.httpMethod = "GET"
var session: URLSession = URLSession.shared
let task = session.dataTask(with: request) { (data, response, error) -> Void in
// Your completion handler code goes here
}
task.resume()
}
最大的变化是,在 swift 3 中,您不再需要可变的 url 请求,它现在可以作为 var 正确处理。然后您现在使用 URLSession 发送该请求。
这是 URL 请求的典型结构。 GET
请求是默认请求,因此您不需要 NSMutableURLRequest
来指定它。
if let url = URL(string: URLWarrick) {
let request = URLRequest(url: url)
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
if let error = error {
print(error.localizedDescription)
return
}
if let data = data {
if let json = try? JSONSerialization.jsonObject(with: data) {
if let dict = json as? Dictionary<String,Any> {
if let teams = dict["teams"] as? Array<Dictionary<String,Any>> {
for team in teams {
guard
let id = team["id"] as? Int,
let name = team["name"] as? String,
let member = team["memeber"] as? Int
else {
print("Error! - Data for this team is incomplete.")
continue
}
print(id, name, member)
}
return
}
}
}
}
print("Error! - Your data was invalid.")
})
task.resume()
}