首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从MySQL检索数据并显示它

从MySQL检索数据并显示它
EN

Stack Overflow用户
提问于 2016-06-11 08:59:37
回答 1查看 104关注 0票数 0

我在做我的私人项目时遇到了一个问题。我刚刚买了一台虚拟专用服务器(VPS)来改善我的环境。我安装了Apache,MySQL和PHP,以便能够将数据保存在数据库中,然后将其解析为PHP,最后在我正在创建的iOS应用程序上显示它。

我在互联网上浏览了一些关于如何从php文件中检索数据并将其显示在tableView中的教程,这就是我遇到的第一个问题。我不打算使用tableView,而且出于某种原因,我一直在努力将数据放入字典/数组中,而没有任何问题。

问题:我遵循了一个教程,并使其适用于tableView,但是当我试图定制输出时,我没有设法得到正确的结果。

我得到了要保存到字典中的信息,但是当我尝试以任何方式使用字典时,我得到了一个断点,模拟就停止了。

代码:

Class - LocationModel.swift

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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,但在那之后它就崩溃了。

我真的很感谢在这件事上的一些帮助,因为我很快就会因为这个问题而头晕目眩。如果你觉得解决方案不好,或者想要更多的信息,请尽管问。

EN

回答 1

Stack Overflow用户

发布于 2016-06-12 07:38:40

我觉得我已经解决了这个问题,但我不确定这是否是最终的解决方案。我将以下部分从parserJSON移到了URLSession。

来自parseJSON

代码语言:javascript
运行
复制
var jsonResult: NSMutableArray = NSMutableArray()

            do {
                jsonResult = try NSJSONSerialization.JSONObjectWithData(self.data, options:NSJSONReadingOptions.AllowFragments) as! NSMutableArray
            } catch let error as NSError {
                print(error)
            }

要使用URLSession函数

代码语言:javascript
运行
复制
 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)
            }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37758960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档