【发布时间】:2016-06-11 00:59:36
【问题描述】:
我在处理我的私人项目时遇到了一个问题。我刚刚通过购买虚拟专用服务器 (VPS) 改善了我的环境。我安装了 Apache、MySQL 和 PHP,以便能够将数据保存在数据库中,然后将其解析为 PHP,最后在我正在创建的 iOS 应用程序上显示它。
我浏览了互联网上关于如何从 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()。我尝试在调试中搜索更多信息,并在线程上使用 po $arg0 来尝试查找更多信息,但我只收到错误消息Errored out in Execute, couldn't PrepareToExecuteJITExpression
我确实得到了充满信息的jsonResult NSMutableArray 和jsonElement,但之后它就坏了。
我真的很感激能在这件事上得到一些帮助,因为我很快就会因为这个问题而脱发。如果您发现解决方案不好或想了解更多信息,请询问。
【问题讨论】:
-
这里没有 PHP。它与问题直接相关还是只是偶然的?
-
文件/URL 可以在 HomeModel.class 变量
url中找到。我可以通过它的一部分,因为它很大。 server.truesight.se/risSwiftPHP/phptest.php[{"id":"8730","name":"7-Eleven 4521117","address":"Drottninggatan 106","city":"Stockholm","country":"Sweden","typ":"Butik med beredning","lastresult":"\u00c5terbes\u00f6k kr\u00e4vs"},{AND SO ON}