[英]Unwrapping a value using SwiftJSON results in an error on iOS13
我有一個提供數據的外部 api,我需要在我的視圖中解碼並分配給一個標簽。
我在 collectionView 中使用以下內容,所以不要介意collectionView.tag
和indexPath.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.