gpt4 book ai didi

ios - 无法将类型 '__NSDictionaryM' (0x10b708d38) 的值转换为 'NSMutableArray' (0x10b708978)。并且无法在 Collection View 中查看我的数据

转载 作者:行者123 更新时间:2023-11-30 13:33:49 24 4
gpt4 key购买 nike

我尝试获取数据并在我的 Collection View 中显示。但是当我尝试显示时。我收到一些错误。并且无法显示。另外请查看下面的 json 结构。它看起来像在我的数据下只需我需要获取类型名称并必须显示在我的 Collection View 中。

这是我的第一个ios。请帮我解决我遇到的问题。

这是我的代码:

import UIKit

class popVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {


var accessToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyIkX18iOnsic3RyaWN0TW9kZSI6dHJ1ZSwiaW5zZXJ0aW5nIjp0cnVlLCJnZXR0ZXJzIjp7InNvY2lhbF9saW5rcyI6e319L"




var arrCol :NSMutableArray=[]


override func viewDidLoad() {
super.viewDidLoad()
self.jsonParsingFromURL()
// Do any additional setup after loading the view, typically from a nib.

}
// collection view delegate

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrCol.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: colvwCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! colvwCell


cell.lblCell.text = arrCol[indexPath.section] .valueForKey("type") as? String

return cell
}



func jsonParsingFromURL () {
let url = NSURL(string: "http://someurl/data?accessToken=\(accessToken)")
let dataTask = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) -> Void in
print("done, error: \(error)")

if error == nil
{

dispatch_async(dispatch_get_main_queue()){
self.arrCol=(try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSMutableArray

print(self.arrCol)

}

}


}
dataTask.resume()
}



}

这是我的 json 结构:

   {
"success": 1,
"data": [
{
"_id": "56c8d71999e4d5cd268d9eca",
"type": "park",

},
{
"_id": "56cbeb01425448492ad528ce",
"type": "medical",


},
{
"_id": "56cbeb9d6deb684f2a140e5e",
"type": "home",

},
{
"_id": "56cbec2beff2bf552a0447c4",
"type": "kitchen",

},
{
"_id": "56cbed2e1c856c672a302ce4",
"type": "bloare",

}
]
}

最佳答案

要获取 data 键的数组并同时检查键 success 的值是否为 1,请使用此

if error == nil {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String:AnyObject]
if let dataArray = json["data"] as? [[String:String]], success = json["success"] as? Int where success == 1 {
self.arrCol = dataArray
print(dataArray)
dispatch_async(dispatch_get_main_queue()) {
// update here the UI.
}
}
} catch let error as NSError {
print(error)
}
}

并将 arrCol 声明为原生 Swift 数组

var arrCol = [[String:String]]()

关于ios - 无法将类型 '__NSDictionaryM' (0x10b708d38) 的值转换为 'NSMutableArray' (0x10b708978)。并且无法在 Collection View 中查看我的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36306873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com