簡體   English   中英

無法將“(String, JSON)”類型的值轉換為預期的參數類型“[JSONSubscriptType]”

[英]Cannot convert value of type '(String, JSON)' to expected argument type '[JSONSubscriptType]'

我試圖遍歷一組帖子並通過遍歷圖像計數來獲取他們的媒體。

這不起作用 - images被設置為0

for images in JSON(response.value!)["post"]["media"] {
    print(JSON(response.value!)["post"]["media"][images]["image_url"])
}

但這確實

for images in JSON(response.value!)["post"]["media"] {
    print(JSON(response.value!)["post"]["media"][0]["image_url"])
}

我得到的錯誤是

Cannot convert value of type '(String, JSON)' to expected argument type '[JSONSubscriptType]'

似乎使用循環中的變量不起作用,但直接 Int 起作用。 我也試過將images包裝為Int

工作代碼顯示關鍵media的對象是一個字典數組。

所以在循環中images (實際上是image )代表每次迭代中陣列media一項。

假設JSONSwiftyJSON相關—— SwiftyJSON ,它已經過時而支持Codable你必須編寫

for image in JSON(response.value!)["post"]["media"].arrayValue {
    print(image["image_url"])
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM