gpt4 book ai didi

ios - 解析 json 数组 swift 返回空元素

转载 作者:行者123 更新时间:2023-11-28 09:36:04 25 4
gpt4 key购买 nike

根据 OOPer 的建议,我将其作为一个单独的问题发布。这是对此的扩展: JSONserialization error

我正在从一个读取 sql 数据库的 php 脚本中提取一个 json 数组。 json 相当大,但应该使用多个在线测试器进行测试,格式正确。问题是,当我尝试从数组中取出元素时,它返回 nil。它能够下载数据,并且可以正确计算数组中有多少元素,但是当我尝试访问元素时,它返回 nil。有什么建议吗?

这是一个元素的例子:

{
"id":2045,
"oprettelsesdato":"09-02",
"overskrift":"etc etc etc",
"navn":"xyz xyz xyz",
"tlf":"12345678",
"email":"etc@etc.etc",
"journal":"MJ",
"intro":"yada yada yada yada ",
"annonce":"test",
"address":"testroad"
},

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)"

}



}

这里是抛出错误的地方:

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

}

错误信息:

fatal error: unexpectedly found nil while unwrapping an Optional value

更新所以我在 json 解析器中将其缩小为以下内容。尽管所有 jsonElements 都包含值,但 if let optional 永远不会为真。怎么了?

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)

}

最佳答案

如果行

extralabel!.text = item.oprettelsesdato // <-This is where the error is thrown 

如果抛出“unexpectedly found nil while unwrapping an Optional value”错误,那么原因几乎可以肯定是 extralabel(应命名为 extraLabel 以遵循驼峰式命名约定)为 nil。在该行设置断点并检查 extralabel

关于ios - 解析 json 数组 swift 返回空元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42177591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com