从MySQL检索数据并显示它

时间:2016-06-11 00:59:37

标签: php mysql json swift

我在处理私人项目时遇到了问题。我只是通过购买虚拟专用服务器(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)"
    }
}

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,但之后它就会中断。

我真的很感激这方面的一些帮助,因为这个问题,我很快就会因为头发而烦恼。如果您发现解决方案不好或想要了解更多信息,请询问。

1 个答案:

答案 0 :(得分:0)

我觉得我已经解决了,但我不确定它是不是最终的解决方案。我将以下部分从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)
            }
        }