parsing一个json数组会返回swift中的空元素
正如OOPer所build议的那样,我把它作为一个单独的问题发布。 这是这个的扩展: JSON序列化错误
我拉一个PHP脚本读取一个SQL数据库的JSON数组。 json非常大,但应该通过使用几个在线testing人员进行testing。 问题是,当我尝试从数组中获取元素返回nil。 它能够下载数据,并且可以正确地计算数组中有多less个元素,但是当我尝试访问这些元素时,它将返回nil。 有什么build议么?
这是一个元素的例子:
{ "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
更新所以我缩小到下面的jsonparsing器。 尽pipe所有的jsonElements都包含值,但是让可选是不正确的。 哪里不对?
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) }
根据给定的JSON id
显然是Int
,而不是String
var id: Int ... if let id = jsonElement["id"] as? Int,
注意:当提供一个非可选的初始值设定项时,或者作为一个懒惰函数不要写入一个初始值设定项时,不要声明这些属性是可选的。 Swift的types系统鼓励您尽可能使用非可选types。 非可选types永远不会崩溃的应用程序!
如果行
extralabel!.text = item.oprettelsesdato // <-This is where the error is thrown
是抛出一个“意外地发现零,而解开一个可选值”的错误,那么原因几乎可以肯定,该extralabel
(应该命名为extraLabel遵循驼峰命名约定)是零。 在该行设置一个断点并检查extralabel
。
你的var oprettelsesdato:String?
variables是一个可选的variables。因为你没有任何选项说“有一个值”或“根本就没有一个值” 。如果你定义variables是一个可选的 ,那么要从这个variables获得值,你将不得不解开它。并且强制解包的好方法是可选绑定。这里是一个例子:
var oprettelsesdato:String? oprettelsesdato = "hello swift!" if let value = oprettelsesdato { println(value) } else { println("no value") }
以这种方式尝试您的代码将解决您的问题
if let
和存储arraysvariables值,那么if let
使用,然后将它们分配给您的标签, if let
条件if let
。
例-:
if let value = item.oprettelsesdato { extralabel.text = value } else { print("no value") }
- 转换后的date比较为零
- Swift JSON错误,无法将types'__NSArrayM'(0x507b58)的值转换为'NSDictionary'(0x507d74)
- tableView没有显示来自JSON的数据在swift中
- 设置状态栏?
- Swift + Locksmith:没有获取存储的钥匙串值
- Swift:如何从NSScanner的AutoreleasingUnsafePointer <NSString?>中获取值?
- 自定义input视图作为Swift中使用OSX 10.9和Xcode 6.1.1 +的文本字段的键盘
- Swift在应用程序中添加自定义键盘或数字键盘
- 使用键值对在Swift中进行POST请求