我的 json 数据中有以下参数。
date = "2018-02-12";
"grand_total" = "82.88";
message = "Order Details Details";
status = "order_details";
"sub_total" = 74;
"tax_total" = "8.880000000000001";
time = "06:53:46";
如何为标签设置总计和小计。在 objective c 中,我们可以使用 nsstring string with format 直接访问字符串的任何值,但在 swift 中没有这样的选项。
最佳答案
swift 3
使用 if let
是最安全的方法。
if let gTotal = dict["grand_total"] as? Double {
// gTotal had the double value
}
else if let gTotal = dict["grand_total"] as? Int {
// gTotal had the int value
}
else {
// not double nor int value.
}
if let subTotal = dict["sub_total"] as? Double {
// subTotal had the double value
}
else if let subTotal = dict["sub_total"] as? Int {
// subTotal had the int value
}
else {
// not double nor int value.
}
if let message = dict["message"] as? String {
// message had the string value
}
else {
// not string value
}
希望对您有所帮助。
关于ios - 如何在 Swift 中访问 Json Int、Double、String 中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48741048/