繁体   English   中英

如何在 Swift 中加入这两个数组

[英]How can I join this two array in Swift

{
    "couriers" : [
      {
        "id" : 30,
        "name" : "abc"
      }
    ],
    "new_couriers" : [
      {
        "name_display" : "abc",
        "fee" : "1.00",
        "name" : "ABC Express"
      }
    ]
}

我想将这两个数组连接在一起并显示在 UITableView 中,不确定 append 是否可能。

    struct AllCourier {
        var couriers = [Couriers]()
        var new_couriers = [NewCouriers]() 
    }

    for item in allCourier {
        available_courier.new_couriers = item.new_courier
        available_courier.couriers =  item.couriers
    }

我创建了一个结构并将 class 添加到其中

    func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
        return allCourier.count
    }

    // create a cell for each table view row
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ShippingOptionCell
        let item = allCourier[indexPath.item]

        return cell
    }

你需要创造性地想办法做到这一点。 您不能简单地连接两个拥有不同 object 类的 arrays。

暗示:

class BaseCourier {
    ...
}

class Courier: BaseCourier {
    ...
}

class NewCourier: BaseCourier {
    ...
}

let couriers = [Courier()]
let newCouriers = [NewCourier()]

let allCouriers = couriers + newCouriers

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM