我在做我的私人项目时遇到了一个问题。我刚刚买了一台虚拟专用服务器(VPS)来改善我的环境。我安装了Apache,MySQL和PHP,以便能够将数据保存在数据库中,然后将其解析为PHP,最后在我正在创建的iOS应用程序上显示它。
我在互联网上浏览了一些关于如何从php文件中检索数据并将其显示在tableView中的教程,这就是我遇到的第一个问题。我不打算使用tableView,而且出于某种原因,我一直在努力将数据放入字典/数组中,而没有任何问题。
问题:我遵循了一个教程,并使其适用于tableView,但是当我试图定制输出时,我没有设法得到正确的结果。
我得到了要保存到字典中的信息,但是当我尝试以任何方式使用字典时,我得到了一个断点,模拟就停止了。
代码:
Class - 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)"
    }
}Class - 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,但在那之后它就崩溃了。
我真的很感谢在这件事上的一些帮助,因为我很快就会因为这个问题而头晕目眩。如果你觉得解决方案不好,或者想要更多的信息,请尽管问。
发布于 2016-06-12 07:38:40
我觉得我已经解决了这个问题,但我不确定这是否是最终的解决方案。我将以下部分从parserJSON移到了URLSession。
来自parseJSON
var jsonResult: NSMutableArray = NSMutableArray()
            do {
                jsonResult = try NSJSONSerialization.JSONObjectWithData(self.data, options:NSJSONReadingOptions.AllowFragments) as! NSMutableArray
            } catch let error as NSError {
                print(error)
            }要使用URLSession函数
 func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?){
                if error != nil {
                    print("Failed to download data")
                } else {
                    print("Data downloaded")
                    var jsonResult: NSMutableArray = NSMutableArray()
                do {
                    jsonResult = try NSJSONSerialization.JSONObjectWithData(self.data, options:NSJSONReadingOptions.AllowFragments) as! NSMutableArray
                } catch let error as NSError {
                    print(error)
                }
                // Array is jsonResult, 
                // I sent it to my ViewController to control the output. 
                print(jsonResult)
                var VC = ViewController()
                VC.SomeMethod(jsonResult)
            }
        }https://stackoverflow.com/questions/37758960
复制相似问题