简体   繁体   中英

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"
      }
    ]
}

I want to join this two array together and display in UITableView, not sure is it possible to append it.

    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
    }

I created a struct and add class into it

    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
    }

You need to creatively think of a way to do this. You cannot simply concatenate two arrays which hold different object classes.

Hint:

class BaseCourier {
    ...
}

class Courier: BaseCourier {
    ...
}

class NewCourier: BaseCourier {
    ...
}

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

let allCouriers = couriers + newCouriers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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