将Dictionary <string,any =“”>附加到类数组SWIFT 3

时间:2017-01-10 20:32:05

标签: arrays json swift alamofire

你能告诉我吗?如果有任何方法将Dictionary附加到声明为Array的类?就像var cellData = [CellData [()基本上,我正在解析来自JSON的数据,并且我正在尝试在这个类中传递for循环,然后将此数据放在viewDidLoad()中的MainVC中以便更新我的细胞。我即将学习Swift,所以我不确定我是否做得对,以及如何解决问题。

当我尝试追加时,错误显示'无法转换类型的值'字典,字符串,任意&gt;'预期参数类型'CellData'

func downloadBlogDetails(completed: @escaping DownloadComplete) {

    Alamofire.request(CURRENT_URL).responseJSON { response in
        let result = response.result

        if let dict = result.value as? Dictionary<String, Any> {

            if let items = dict["items"] as? [Dictionary<String, Any>] {

                for obj in items {

                    if let title = items[0]["title"] as? String {
                       self._authorName = title
                        print(self._authorName)
                    }

                    if let published = items[0]["published"] as? String {
                        self._publishedDate = published
                        print(self._publishedDate)
                    }

                    if let author = items[0]["author"] as? Dictionary<String, Any> {
                        if let name = author["displayName"] as? String {
                            self._authorName = name
                            print(self._authorName)
                        }
                    }

                    // add element obj to class array
                    self.cellData.append(obj)
                }

            }
        }

        completed()
    }
}

0 个答案:

没有答案