gpt4 book ai didi

php - Swift:无法从 PHP 正确获取数据

转载 作者:行者123 更新时间:2023-11-28 08:25:43 26 4
gpt4 key购买 nike

我正在创建一个应用程序,我在其中从 php.ini 获取数据。要从 php 下载数据,我有一个函数可以下载数据,提交给模型,然后在需要时检索它。现在的问题是,当我的数据从数据库中获取时,它没有适本地同步并且在每个地方都获得相同的信息。例如,Name 显示手机;街道地址也显示相同。

这是我的代码

    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

截图 enter image description here

enter image description here

最佳答案

让我知道这项工作是否适合您,

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)

关于php - Swift:无法从 PHP 正确获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40077337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com