将数据追加到数组时调用中的额外参数

时间:2018-09-25 17:03:29

标签: ios arrays swift

我有一个自定义表格单元,其中包含一个图像和一些字符串。现在,我进行异步调用以获取可以正常工作的JSON。得到我的JSON后,我试图将其追加到我的数组,但我在调用中得到Extra参数。下面是我的示例代码:

//构造数组详细信息

struct CellData {
    let image: UIImage?
    let name: String?
    let distance: String?
    let desc: String?
}
var locationData = [CellData]()
//Then in my background thread

let responseMessage = parseJSON["RESPONSEMESSAGE"] as! NSArray?
if let array = responseMessage {
    for obj in array{
        if let dict = obj as? NSDictionary {
            let shopName = dict.value(forKey: "NAME") as? String
            let shopLandMark = dict.value(forKey: "LANDMARK") as! String?
            var shopDistance = dict.value(forKey: "DISTANCE") as! String?
            shopDistance = "\(shopDistance) km"
            DispatchQueue.main.async {
                self.locationData.append(image: #imageLiteral(resourceName: "wifi"), shopName!, shopDistance!, shopLandMark!) //Error on this line (Extra argument in call)
            }


        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的append仅接受一个参数-类型为{=INDEX($E$7:$E$13; SMALL(IF($H$7:$H$12 & $G$7:$G$12 = "Not PaidDebit"; ROW($H$7:$H$12)-ROW($H$7)+1); 1) )} 的实例。

您需要创建CellData类型的实例,并将该实例附加到数组中。

CellData

如果您的响应API发生变化,并且您的密钥没有为您提供值,则您的应用将崩溃。因此,请安全地使用您的价值观。避免使用DispatchQueue.main.async { for obj in array{ if let dict = obj as? [String: String] { if let shopName = dict["NAME"] as? String, let shopLandMark = dict.["LANDMARK"] as? String?, let shopDistance = dict["DISTANCE"] as? String? { let shopLocation = CellData(image: #imageLiteral(resourceName: "wifi"), name: shopName, distance: shopDistance, desc: shopLandMark) locationData.append(shopLocation) } } } tableView.reloadData() } NSArray。改用NSDictionaryArray