As suggested by OOPer I am posting this as a separate question. This is an extension of this: JSONserialization error
I am pulling a json array from a php script that reads an sql database. The json is pretty large but should be well formed as tested using several online testers. The problem is that when I try to get the elements out of the array it returns nil. It is able to download the data and it can correctly count how many elements are in the array but when I try to access the elements it returns nil. Any suggestions?
Here is an example of an element:
{
"id":2045,
"oprettelsesdato":"09-02",
"overskrift":"etc etc etc",
"navn":"xyz xyz xyz",
"tlf":"12345678",
"email":"[email protected]",
"journal":"MJ",
"intro":"yada yada yada yada ",
"annonce":"test",
"address":"testroad"
},
The LocationModel.swift
import Foundation
class LocationModel: NSObject {
//properties
var id: String?
var oprettelsesdato: String?
var overskrift: String?
var address: String?
var intro: String?
var email: String?
var tlf: String?
var annonce: String?
var journalnr: String?
override init()
{
}
init(id: String, oprettelsesdato: String, overskrift: String, address: String, intro: String, email: String, tlf: String, annonce: String, journalnr: String) {
self.id = id
self.oprettelsesdato = oprettelsesdato
self.overskrift = overskrift
self.address = address
self.intro = intro
self.email = email
self.tlf = tlf
self.annonce = annonce
self.journalnr = journalnr
}
override var description: String {
return "id: \(id), oprettelsesdato: \(oprettelsesdato), overskrift: \(overskrift), address: \(address), journalnr: \(journalnr)"
}
}
And here is where the error is thrown:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item: LocationModel = feedItems[indexPath.row] as! LocationModel
let cellIdentifier: String = "BasicCell"
let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
print(feedItems[indexPath.row])
//myCell.detailTextLabel!.text = item.oprettelsesdato
myCell.textLabel!.text = item.overskrift
myCell.textLabel!.leadingAnchor.constraint(equalTo: myCell.leadingAnchor).isActive = true
myCell.textLabel!.topAnchor.constraint(equalTo: myCell.topAnchor).isActive = true
myCell.textLabel!.heightAnchor.constraint(equalTo: myCell.heightAnchor,
multiplier: 1.0).isActive = true
myCell.textLabel!.widthAnchor.constraint(equalTo: myCell.heightAnchor,
multiplier: 0.8).isActive = true
//print(item.id) <-returns nil
//print(item.oprettelsesdato) <-returns nil
//print(item.overskrift) <-returns nil
extralabel!.text = item.oprettelsesdato // <-This is where the error is thrown
}
error msg:
fatal error: unexpectedly found nil while unwrapping an Optional value
update So I narrowed it down to the following in the json parser. The if let optional is never true although all the jsonElements contain values. What is wrong?
for jsonElement in jsonResult {
print(jsonElement["id"]) //<- these print() all return the correct values
print(jsonElement["oprettelsesdato"])
print(jsonElement["overskrift"])
print(jsonElement["address"])
print(jsonElement["intro"])
print(jsonElement["email"])
print(jsonElement["tlf"])
print(jsonElement["annonce"])
print(jsonElement["journal"])
let location = LocationModel()
if let id = jsonElement["id"] as? String,
let oprettelsesdato = jsonElement["oprettelsesdato"] as? String,
let overskrift = jsonElement["overskrift"] as? String,
let address = jsonElement["address"] as? String,
let intro = jsonElement["intro"] as? String,
let email = jsonElement["email"] as? String,
let tlf = jsonElement["tlf"] as? String,
let annonce = jsonElement["annonce"] as? String,
let journalnr = jsonElement["journal"] as? String
{ //this never returns true and location. is never set. Why??
location.id = id
location.oprettelsesdato = oprettelsesdato
location.overskrift = overskrift
location.address = address
location.intro = intro
location.email = email
location.tlf = tlf
location.annonce = annonce
location.journalnr = journalnr
}
locations.append(location)
}