我正在创建一个应用程序,我从php获取数据。要从php下载数据,我有一个func可以下载数据,提交给模型,然后在需要时检索它。现在的问题是,当我从数据库中获取数据时,它不是适当的snyc并且每个地方都获得相同的信息。例如,姓名显示移动电话;街道地址也显示相同。
这是我的代码
func download(){
let request = NSMutableURLRequest(url: URL(string: "http://www.some-site.com")!)
request.httpMethod = "POST"
let postString = "id=\(businessID)"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: (request as URLRequest), completionHandler: { data, response, error in
let responseString = String(data: data!, encoding: String.Encoding.utf8)
let r = self.convertStringToDictionary(responseString!)
for element in r! {
print("element = \(element)")
let stname = String(describing: element.first!)
let sn = stname.replacingOccurrences(of: "\"STREET_NAME\", ", with: "")
let name = String(describing: element.first!)
let n = name.replacingOccurrences(of: "\"FILE_LOCATION\", ", with: "")
let reverse = element.reversed()
let fileloc = String(describing: reverse.first!)
let f = fileloc.replacingOccurrences(of: "\"BUSINESS_NAME\", ", with: "")
self.model = ProfilePage(fileloc: f, streetname: sn, name: n)
print("Address = \(stname)")
print("Name = \(name)")
print("File Location = \(f)")
DispatchQueue.main.async {
self.setUI()
}
}
if error != nil {
print("error=\(error)")
}
})
task.resume()
}
}
but yes in my output console, i can see all the infor appropriate, its just when i run this, it shows all the info as one
答案 0 :(得分:0)
让我知道这项工作适合你,
let r = self.convertStringToDictionary(responseString!) // Make sure Your string is successfully converted as NSDictionary else use below
// let r = self.convertStringToDictionary(responseString!) as NSDictionary // if r is dictionary, then convert to NSDictionary
let stname = r["STREET_NAME"] as! String
let name = r["FILE_LOCATION"] as! String
let f = r["BUSINESS_NAME"] as! String
print("Address",stname)
print("Name",name)
print("File Location",f)
DispatchQueue.main.async {
self.setUI()
}
如果您混淆,请参阅以下示例
let mydict = ["Hip-Hop Tamizha": 21,"Hip-Hop Tamizha":"Takkaru Takkaru","Michael Jackson":"Beat It","Taylor Swift":"Back to December","Katy Perry":"Fire Works","Selina Gomez":"Love You Like A Love Song Baby","Avril Lavigne":"Slipped Away","Eminem":"The Music Box","Akhil":"Khaab","Hip-Hop Tamizha": 21,"Akhil": 61] as NSDictionary
let hop = mydict["Hip-Hop Tamizha"] as AnyObject
let swift = mydict["Taylor Swift"] as! String
print("The Number:",hop)
print("Taylor Swift song:",swift)