我在处理私人项目时遇到了问题。我刚刚通过购买虚拟专用服务器(VPS)改善了我的环境。我安装了Apache,MySQL和PHP,以便能够将数据保存到数据库中,然后将其解析为PHP,并在最终阶段在我创建的iOS应用程序上显示它。从MySQL中检索数据并显示它
我浏览了很多互联网上的教程,关于如何从php文件中检索数据并将其显示在tableView中,这是我遇到的第一个问题。我不打算使用tableView,出于某种原因,我一直在努力将数据放入字典/数组中而没有任何问题。
问题: 我跟着一个教程,并使它适用于tableView,但是当我试图自定义输出时,我无法做到正确。
我得到的信息被保存到字典中,但是当我尝试以任何方式使用字典时,我得到一个断点,仿真停止。
代码:
类 - LocationModel.swift
import Foundation
class LocationModel: NSObject {
var id: String?
var name: String?
var address: String?
var city: String?
var country: String?
var typ: String?
var lastresult: String?
override init() {
}
init(id: String, name: String, address: String, city: String, country: String, typ: String, lastresult: String) {
self.id = id
self.name = name
self.address = address
self.city = city
self.country = country
self.typ = typ
self.lastresult = lastresult
}
override var description: String {
return "Name: \(name), Address: \(address)"
}
}
类 - HomeModel.swift
import Foundation
protocol HomeModelProtocal: class {
func itemsDownloaded(items: NSArray)
}
class HomeModel: NSObject, NSURLSessionDataDelegate {
weak var delegate: HomeModelProtocal!
var data : NSMutableData = NSMutableData()
let urlPath: String = "http://server.truesight.se/risSwiftPHP/phptest.php"
func downloadItems() {
let url: NSURL = NSURL(string: urlPath)!
var session: NSURLSession!
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)
let task = session.dataTaskWithURL(url)
task.resume()
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
self.data.appendData(data)
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?){
if error != nil {
print("Failed to download data")
} else {
print("Data downloaded")
self.parseJSON()
}
}
func parseJSON() {
var jsonResult: NSMutableArray = NSMutableArray()
do {
jsonResult = try NSJSONSerialization.JSONObjectWithData(self.data, options:NSJSONReadingOptions.AllowFragments) as! NSMutableArray
} catch let error as NSError {
print(error)
}
var jsonElement: NSDictionary = NSDictionary()
let locations: NSMutableArray = NSMutableArray()
for item in jsonResult {
jsonElement = item as! NSDictionary
let location = LocationModel()
if let id = jsonElement["id"] as? String,
let name = jsonElement["name"] as? String,
let address = jsonElement["address"] as? String,
let city = jsonElement["city"] as? String,
let country = jsonElement["country"] as? String,
let typ = jsonElement["typ"] as? String,
let lastresult = jsonElement["lastresult"] as? String
{
print(id + name + address + country + city + typ + lastresult)
location.id = id
location.name = name
location.address = address
location.city = city
location.country = country
location.typ = typ
location.lastresult = lastresult
}
if let name = jsonElement["name"] as? String,
let address = jsonElement["address"] as? String
{
location.name = name
location.address = address
}
locations.addObject(location)
}
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.delegate.itemsDownloaded(locations)
})
}
}
它是未在断点出现的类HomeModel的方法/函数parseJSON 。这是破坏代码的let location = LocationModel()
。我试图通过调试的详细信息,搜索并一直在使用的螺纹婆$ arg0都试图找到更多的,但我只能得到错误信息Errored out in Execute, couldn't PrepareToExecuteJITExpression
我得到了jsonResult
NSMutableArray里充满了信息和jsonElement
为好吧,但之后就会中断。
我真的很感谢在这件事情上的一些帮助,因为我很快就因为这个问题而头发不整。如果您发现解决方案不好或需要更多信息,请直接询问。
这里没有PHP。它与问题直接相关还是偶然? – tadman
可以在HomeModel.class变量'url'中找到文件/ URL。我可以通过它的一部分,因为它很大。 http://server.truesight.se/risSwiftPHP/phptest.php '[{“id”:“8730”,“name”:“7-Eleven 4521117”,“address”:“Drottninggatan 106”,“city “:”斯德哥尔摩“,”国家“:”瑞典“,”典型“:”Butik med beredning“,”lastresult“:”\ u00c5terbes \ u00f6k kr \ u00e4vs“},{AND SO ON} – TonyLanglet