我收到这个错误:
Could not cast value of type 'NSNull' (0x1114e4850) to 'NSString' (0x11065d2a8).
//These are my arrays
var snoArray:[Int] = []
var quote_idArray:[String] = []
var quote_amountArray:[String] = []
var dateArray:[String] = []
var timeArray:[String] = []
var perkilometerArray:[String] = []
var pertonArray:[String] = []
//My JSON response is like this.
["Response": {
Array = (
{
date = "25-05-2018";
perkilometer = 2;
perton = "<null>";
"quote_amount" = 1234;
"quote_id" = 5b080f8aa082c;
sno = 1;
time = "18:58";
},
{
date = "25-05-2018";
perkilometer = 346;
perton = "<null>";
"quote_amount" = 230000;
"quote_id" = 5b080ed57aa34;
sno = 2;
time = "18:55";
}
);
"error_code" = 0;
message = SUCCESS;
status = SUCCESS;
}, "count": 2]
//I'm catching response like this...
if status == "SUCCESS" {
if let array = res!["Array"] as? Array<Dictionary<String, Any>> {
// print(array.count)
self.snoArray = array.map { $0["sno"]! } as! [Int]
self.quote_idArray = array.map { $0["quote_id"]! } as! [String]
self.quote_amountArray = array.map { $0["quote_amount"]! } as! [String]
self.dateArray = array.map { $0["date"]! } as! [String]
self.timeArray = array.map { $0["time"]! } as! [String]
self.perkilometerArray = array.map { $0["perkilometer"]! } as! [String]
//But here I'm getting null values and app crashed
self.pertonArray = array.map { $0["perton"]! } as! [String]
}
}
//When I write like this, it's printing : [<null>, <null>]
let perTon = array.compactMap({ $0 ["perton"]})
print(perTon) // Output: [<null>, <null>]
我提到了这个链接:https://useyourloaf.com/blog/swift-non-nil-values-in-an-array-of-optionals/
在这里,我想从self.pertonArray中删除空值,并希望打印空self.pertonArray([,])。
如果假设我得到这样的响应如何解决这个问题。
["Response": {
Array = (
{
date = "25-05-2018";
perkilometer = 2;
perton = "something";
"quote_amount" = 1234;
"quote_id" = 5b080f8aa082c;
sno = 1;
time = "18:58";
},
{
date = "25-05-2018";
perkilometer = 346;
perton = "<null>";
"quote_amount" = 230000;
"quote_id" = 5b080ed57aa34;
sno = 2;
time = "18:55";
}
);
"error_code" = 0;
message = SUCCESS;
status = SUCCESS;
}, "count": 2]
如何解决这个问题呢....
绑定时有很多方法可以从JSON中删除nil。但根据你的代码,这里有一个。
if status == "SUCCESS" {
if let array = res!["Array"] as? Array<Dictionary<String, Any>> {
// print(array.count)
self.snoArray = array.map { ($0["sno"] as? Int) ?? 0 }
self.quote_idArray = array.map { ($0["quote_id"] as? String) ?? "" }
self.quote_amountArray = array.map { ($0["quote_amount"] as? String) ?? "" }
self.dateArray = array.map { ($0["date"] as? String) ?? "" }
self.timeArray = array.map { ($0["time"] as? String) ?? ""}
self.perkilometerArray = array.map { ($0["perkilometer"] as? String) ?? "" }
// You get error here with null values
// here is how it's fixed
self.pertonArray = array.map { ($0["perton"] as? String) ?? "" }
}
}
在绑定json时不使用Force Unwrapping
是一个很好的做法,因为你永远不会确定没有null或不正确的数据(你期望Int但是值是从服务器发送的String)。
使用字符串插值来解析值
比如你的回答
你想得到perton
let value = ((responce as! Array)[indexofarray] as! Dictionary<string,any>)["perton"]
let perton = "\(value ?? "")"
这将返回第一个索引的something
和第二个索引,它将返回字符串中的<null>