繁体   English   中英

在 iOS13 上使用 SwiftJSON 解包一个值会导致错误

[英]Unwrapping a value using SwiftJSON results in an error on iOS13

我有一个提供数据的外部 api,我需要在我的视图中解码并分配给一个标签。

我在 collectionView 中使用以下内容,所以不要介意collectionView.tagindexPath.row

我也使用SwiftyJson来解析 json 值.string提供和可选值

let value = servicesResponse["data"][collectionView.tag]["subcategories"][indexPath.row]["name"].string

现在,当我尝试将此值分配给标签时

cell.serviceName.text = value

我收到一个错误:

'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithString:]: nil argument'

分配时,我必须将值放入这样的内容中:

`"\(value)"`

这工作正常,但它在值周围有Optional文本。

我也尝试过:

  • 展开值cell.serviceName.text = value! 给出同样的错误
  • 使用.stringValue而不是.string给出了非可选值给出了相同的错误 -used .rawString而不是.string给出了相同的错误
  • 尝试以这种方式解开它
if let value = servicesResponse["data"][collectionView.tag] 
   ["subcategories"][indexPath.row]["name"].string {
        cell.serviceName.text = value
}

同样的错误

  • 并尝试给它一个默认值cell.serviceName.text = value ?? "default" cell.serviceName.text = value ?? "default"相同的错误

我只是试图通过这样的方式检查所有响应:

if servicesResponse["data"] != JSON.null{
            if servicesResponse["data"][collectionView.tag] != JSON.null{
                if servicesResponse["data"][collectionView.tag]["subcategories"] != JSON.null{
                    if servicesResponse["data"][collectionView.tag]["subcategories"][indexPath.row] != JSON.null{
                        if servicesResponse["data"][collectionView.tag]["subcategories"][indexPath.row]["name"] != JSON.null{
                            print("ALL PASS=========================")
                        }
                    }
                }
            }
        }

和所有的通行证

我想知道是否有办法删除文本Optional而该值仍然是可选的,或者是否有人知道此错误的含义及其发生的原因。

该错误仅发生在iOS13 在早期版本上工作正常

谢谢。

value变量是一个 Optional 字符串,这就是它在打印时显示Optional()的原因。

要解开可选项,您可以执行以下操作:

if let unwrappedValue = value {
    cell.serviceName.text = unwrappedValue
}

或者,如果您愿意,可以使用 nil 合并运算符在一行中完成:

cell.serviceName.text = value ?? "Text in case of nil"

这是因为您的值是可选的。

尝试通过这种方式

if let value = servicesResponse["data"][collectionView.tag]["subcategories"][indexPath.row]["name"] {
    cell.serviceName.text = value.stringValue
}

该错误显示一个字符串被初始化为 nil 参数,因此 json 数据或解码方式不太正确。

你为什么不分阶段解包,而不是一个班轮。 当您处理未知错误时,最好将过程分解为小块,然后逐个检查。 当您发现问题出在哪里时,您可以随时返回到一个班轮,应用您学到的知识。

这就是我想出(并测试)来说明在渐进式 JSON 解码过程中进行调试的一种方法的方法。

给定一个原始的 json 字符串:

let rawString = """
{"statusCode":200,"message":"Success","data":[{"_id":"someid","isActive":true,"subcategories":[{"_id":"id","photo":"image/url/30973327066756228460065.png","services":[{"_id":"id","properties":[{"_id":"id","isMultiSelect":false,"responses":["1","2","3","4","5"],"other_name":"የክፍሎች ቁጥር","name":"Number of rooms"}],"other_name":"ጽዳት","name":"Cleaning"}],"other_name":"ጽዳት","name":"Cleaning","other_subcategory_label":"ጽዳት","subcategory_label":"Cleaning"}],"name":"Home Cleaning","__v":0,"other_name":"የቤት ጽዳት"}]}
"""

我们可以逐渐解码、验证,然后在最后解开名称值。

let parsedJson = JSON.init(parseJSON: rawString)

let verifiedDataJson = parsedJson["data"]
guard verifiedDataJson != JSON.null else {
    return
}

let verifiedCollectionViewTagJson = verifiedDataJson[0]
guard verifiedCollectionViewTagJson != JSON.null else {
    return
}

let verifiedSubCategoriesJson = verifiedCollectionViewTagJson["subcategories"]
guard verifiedSubCategoriesJson != JSON.null else {
    return
}

let verifiedIndexPathRowJson = verifiedSubCategoriesJson[0]
guard verifiedIndexPathRowJson != JSON.null else {
    return
}

guard let unwrappedNameValue = verifiedIndexPathRowJson["name"].string as String? else {
    return
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM