簡體   English   中英

[NSDictionary] !? 不能轉換為[NSDictionary]?

[英][NSDictionary]!? is not convertible to [NSDictionary]?

我正進入(狀態

'[NSDictionary] !? 不能轉換為[NSDictionary]?” 以下代碼錯誤。

  var jsonResult:[NSDictionary]! = [NSJSONSerialization.JSONObjectWithData(urlData, options:NSJSONReadingOptions.MutableContainers, error: &error1)] as? [NSDictionary]!

為什么會這樣呢?

首先:刪除NSJSONSerialization.JSONObjectWithData周圍的[] ,如果將其放在數組中,將很難進行轉換。

使用以下

var jsonResult = NSJSONSerialization.JSONObjectWithData(urlData, options:NSJSONReadingOptions.MutableContainers, error: &error1) as! NSDictionary

as? (可選)強制轉換為給定類型。 因此,您最終得到[NSDictionary]!類型的可選[NSDictionary]! -> [NSDictionary]!? 實際上不能轉換為[NSDictionary]! 但無法包裹。 正確地進行投影和展開是沒有意義的,只是首先使用展開的投影。

蘋果文檔在有關可選項的情況下確實很有幫助!

關於第一點的更多解釋:您將NSJSONSerialization.JSONObjectWithData的返回值NSJSONSerialization.JSONObjectWithData數組中,這將導致[AnyObject?]不能真正轉換為[AnythingElse],因為您必須在轉換前解開可選參數。 我不知道這樣做的任何內置方法。 而且在您的情況下這沒有任何意義。 您仍然可以在所有強制轉換之后將值包裝在數組中,這可以通過

var jsonResult = [NSJSONSerialization.JSONObjectWithData(urlData, options:NSJSONReadingOptions.MutableContainers, error: &error1) as! NSDictionary]

暫無
暫無

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

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