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

标签 php ios swift

我正在创建一个应用程序,我在其中从 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/

相关文章:

php - MYSQL根据jquery可排序的特定顺序进行更新

php - woocommerce 自定义产品页面上的“添加到购物车”按钮和“立即购买”按钮

ios - 如何获取 UISearchBar 的默认灰色

ios - 在函数完成后做一些事情

swift - 在另一个 TableView 上调用 dismiss() 后刷新 TableView 数据

php - 在 PHP 中四舍五入以达到 100%

php - 如何创建临时 url 以防止 php 中的热链接?

ios - 进度的宽度和高度查看

iOS::导航栏上的按钮仅出现在 Root View Controller 上

ios - NSNumberFormatter paddingPosition 不起作用