簡體   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