如何展开可选的不展开显示---精度:可选(“ 2.5”)---

时间:2019-04-22 09:05:50

标签: ios swift

我正在尝试从字典和数组中获取数据到我的自定义结构中进行保存,但是当我打印它时,它的值是在optional(value)中给出的,并且没有将其保存到TableView单元格中出现此问题:(

func findAllShops(){
    Alamofire.request(NEARBY_STORES, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON{ (response) in

        if response.result.error == nil {

            do
            {
                guard let data = response.data else { return }
                if let json = try JSON(data: data).array {
                    for item in json {
                        let storeName = item["name"].stringValue
                        let storeCover = item["cover"].stringValue
                        let storeLogo = item["logo"].stringValue
                        let storeSpeed = item["delievery_time"].stringValue
                        let storeRating = item["average_rating"].stringValue
                        let minimumOrder = item["min_order_amt"].stringValue
                        let shopList = shopModel(shopName: storeName , shopBGImage: storeCover, shoplogo: storeLogo , speed: storeSpeed, accuracy: storeRating, minimumOrder: minimumOrder)
                        self.shopLists.append(shopList)
                    }
                    print(self.shopLists[1])
                }
            }
            catch let error as NSError {
                // error
                print("Error in the parsing")
                debugPrint(response.result.error as Any)
            }
        }
    }
}

预期的输出->

  

shopModel(shopName:(“ Madina Cash and Carry(F11)”),shopBGImage:   (“ uploads / resturant_cover / resturant_4.jpg”),shopLogo:   (“上载/餐厅/resturant_4.jpg”),速度:(“ 24h”),准确性:   (“ 2.5”),minimumOrder:(“ 1000”))

实际输出->

  

shopModel(shopName:Optional(“ Madina Cash and Carry(F11)”),   shopBGImage:可选(“ uploads / resturant_cover / resturant_4.jpg”),   shopLogo:可选(“ uploads / resturants / resturant_4.jpg”),速度:   可选(“ 24h”),准确性:可选(“ 2.5”),最小顺序:   可选(“ 1000”))

3 个答案:

答案 0 :(得分:0)

您应该重做shopModel的struct / class,以便其vars为非可选。

或像下面这样重写代码:

let storeName = item["name"] ?? "" // converts optionals to String
...

答案 1 :(得分:0)

方法:1

  

当您从API收到default值时,只需分配nil值即可。

代码:

  let storeName = item["name"].stringValue ?? ""
  let storeCover = item["cover"].stringValue ?? ""
  let storeLogo = item["logo"].stringValue ?? ""
  let storeSpeed = item["delievery_time"].stringValue ?? ""
  let storeRating = item["average_rating"].stringValue ?? ""
  let minimumOrder = item["min_order_amt"].stringValue ?? ""

方法:2

  

如果API中所有值都是必需的,则遵循此方法。   如果您已从API收到nil值,则   应用程序崩溃

代码:

  let storeName = item["name"].stringValue!
  let storeCover = item["cover"].stringValue!
  let storeLogo = item["logo"].stringValue!
  let storeSpeed = item["delievery_time"].stringValue!
  let storeRating = item["average_rating"].stringValue!
  let minimumOrder = item["min_order_amt"].stringValue!

答案 2 :(得分:0)

//尝试

    let storeName = String.init(format: "%@", item["name"] as! String)
    let storeCover = String.init(format: "%@", item["cover"] as! String)
    let storeLogo = String.init(format: "%@", item["logo"] as! String)
    let storeSpeed = String.init(format: "%@", item["delievery_time"] as! String)
    let storeRating = String.init(format: "%@", item["average_rating"] as! String)
    let minimumOrder =String.init(format: "%@", item["min_order_amt"] as! String)

OR

只需替换您的代码

let shopList = shopModel(shopName: storeName! , shopBGImage: storeCover!, shoplogo: storeLogo! , speed: storeSpeed!, accuracy: storeRating!, minimumOrder: minimumOrder!)