用字典中的'值'填充UITableViewCells的问题>>

问题描述 投票:0回答:1

我有一个iOS应用,正在向我的API发出GET请求以获取所有帖子,这些帖子已保存到数据库中。

我正在尝试填充检索到UITableView单元格中的数据。这是使用的主要代码:

  if let jsonArray = try JSONSerialization.jsonObject(with: 
  data, options : .allowFragments) as? [Dictionary<String,Any>]
   {
                   print(jsonArray) // use the json here

                    //loop through the jsonArray (which holds the objects)
                    for deal in jsonArray {

                        //loop through indivdual objects
                        for (key, value) in deal {

                            var newDeal:Deal!

                            print(key, value)
                            if(key == "location"){
                                newDeal.address = value as! String //after assigning, newDeal is null
                            }
                            if(key == "description"){
                                newDeal.description = value as! String //after assigning, newDeal is null
                            }
                            if(key == "promotion_end"){
                                newDeal.availability = value as! String //after assigning, newDeal is null
                            }
                            if(key == "user_id"){
                                newDeal.poster = value as! String //after assigning, newDeal is null
                            }
                            if(key == "created_at"){
                                newDeal.postedOn = value as! String //after assigning, newDeal is null
                            }
                            self.deals.append(newDeal)//null error
                        }

                        //Example of Deal object:
                        //    Deal(poster: "Company name", postedOn: "2020-02-15", description: "Body of post goes here", availability: "11am-2pm Sat/Sun", address: "300 Somewhere Street")
                    }

我正在遍历jsonArray中的每个对象。我正在检查键的名称,如果键例如是==到'location',然后将与键相关联的值设置为地址,以尝试为每个tableView单元格建立Deal对象当for循环执行时。

为什么赋值后newDeal.address,newDeal.description等为空?请参阅下面有关调试时捕获的几张图像的数据格式。

enter image description here

enter image description here

enter image description here

我有一个iOS应用,正在向我的API发出GET请求以获取所有帖子,这些帖子已保存到数据库中。我正在尝试填充检索到UITableView单元格中的数据。这是主要的...

json swift uitableview nsdictionary
1个回答
0
投票

实际上,您的代码应该崩溃,因为您将Deal声明为隐式未包装的可选内容,但未初始化它。

© www.soinside.com 2019 - 2024. All rights reserved.