Retrieving data from MySQL and Displaying it
<p>I've encountered a problem when I was working on my private project. I just improved my environment by buying a Virtual Private Server (VPS). I installed Apache, MySQL and PHP to be able to save data in a database followed by parsing it to a PHP and the final stage displaying it on an iOS app I'm creating. </p>

<p>I looked through quite few tutorials on the internet on how to retrieve the data from a php file and show it in a tableView and this is where my first problem is encountered. I'm not going to use a tableView and I for some reason have been struggling with just getting the data into an dictionary/array without any issues.</p>

<p>Problem: 
I've followed a tutorial and made it work for a tableView but when I was trying to customize the output I don't manage to get it right. </p>

<p>I get the information to get saved to a dictionary but when I try to use the dictionary in any kind of way i get a breakpoint and the simulation just stops. </p>

<p>Code:</p>

<p>Class - LocationModel.swift</p>

<pre><code>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)"
    }
}
</code></pre>

<p>Class - HomeModel.swift</p>

<pre><code>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(), {() -&gt; Void in

                self.delegate.itemsDownloaded(locations)
            })
        }
    }
</code></pre>

<p>It is un the method/function parseJSON in the class HomeModel where the breakpoint appears. It's the <code>let location = LocationModel()</code> that breaks the code. I've tried to search through the debug for more information and been using po $arg0 on the threads to try find more but I only get the error message <code>Errored out in Execute, couldn't PrepareToExecuteJITExpression</code></p>

<p>I do get the <code>jsonResult</code> NSMutableArray filled with information and the <code>jsonElement</code> as well but after that it breaks. </p>

<p>I would really appreciate some help in the matter as I'm soon out of hair on my head because of this problem. If you find the solution bad or want any more information please just ask.</p>