let todoEndpoint: String ="http://localhost:16789/api/product"
guard let url = NSURL(string: todoEndpoint) else {
print("Error: cannot create URL")
return
}
let urlRequest = NSURLRequest(url: url as URL)
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: urlRequest as URLRequest) {
(data, response, error) in
guard let responseData = data else {
print("Error: did not receive data")
return
}
guard error == nil else {
print("error calling GET")
print(error)
return
}
do{
self.users = try JSONSerialization.jsonObject(with: responseData, options: []) as? Array}
catch{
print("Error parsing")
}
}
task.resume()
这是我的输出我的API [{“ProductId”:2,“类别”:“笔记本电脑”,“品牌”:“Apple”,“ModelName”:“MacBook Pro”,“SerialNumber”:“Test1”,“DatePurchased”:“2016-08- 12T00:00:00“,”EmployeeName“:”Vismita Shetty“,”DateAssigned“:”2017-01-17T00:00:00“},{”ProductId“:4,”类别“:”键盘“,”品牌“:”Logitech“,”ModelName“:”K200“,”SerialNumber“:”TestKeyboard1“,”DatePurchased“:”2017-01-23T03:02:13.247“,”EmployeeName“:”Vismita Shetty“,”DateAssigned“ : “2017-01-17T00:00:00”},{ “产品编号”:5, “类别”: “键盘”, “品牌”: “罗技”, “MODELNAME”: “K200”, “的SerialNumber”:” testkeyboard2“,”DatePurchased“:”2017-01-23T03:03:26.07“,”EmployeeName“:”Suraj Pangam“,”DateAssigned“:”2017-01-17T00:00:00“}]
我目前正在尝试将用户转换为不同的变量,该变量将是swift中的字典数组。这样每个对象都可以通过密钥访问。我想让它变得通用,以便即使某些属性被更改也无关紧要,因为它不会通过提供属性名来访问。喜欢(users [indexPath.row]为?[String:AnyObject])?[“EmployeeName”]为!串!
相反,如果我将它设为字典,我将通过为字典数组运行(key,value)循环来轻松完成。