簡體   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