我目前正在尝试使用 Swift 以 JSON 格式从 mySQL 中检索数据。我意识到NSURL
已更改为URL
. 我正在遵循另一个较旧的代码来帮助指导我完成这个,但它已经过时了。我在let request
和处有错误(NSURL/URL) let task
。我需要一些帮助来弄清楚如何正确地做到这一点。谢谢!
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("")
}