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.
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.
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.
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.
Code:
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)
})
}
}
It is un the method/function parseJSON in the class HomeModel where the breakpoint appears. It's the let location = LocationModel()
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 Errored out in Execute, couldn't PrepareToExecuteJITExpression
I do get the jsonResult
NSMutableArray filled with information and the jsonElement
as well but after that it breaks.
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.
I feel like I've solved it but I'm not sure if it's the ultimate solution. I moved the following section from parserJSON to URLSession.
From parseJSON
var jsonResult: NSMutableArray = NSMutableArray()
do {
jsonResult = try NSJSONSerialization.JSONObjectWithData(self.data, options:NSJSONReadingOptions.AllowFragments) as! NSMutableArray
} catch let error as NSError {
print(error)
}
To function 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)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.