将多个阵列合并/合并为一个

时间:2019-12-17 16:27:57

标签: arrays swift iphone variables merge

我有四个数组列表:

var products: [Product] = []
var addressInfo: [AddressInfo] = []
var favorites: [Favorite] = []
var amounts: [Amount] = []

我想将它们全部合并为一个,所以只有一个变量:

var combined = [Product/AddressInfo/Favorite/Amount]()

如何将它们合并到一个数组var cobmbined = [Combined]()中?

这是两个有关我的结构外观的示例:

struct Amount {
    var amountsDeliveryCharge: Double
    var amountsDriverTip: Double
    init(        
        amountsDeliveryCharge: Double,
        amountsDriverTip: Double
        ){
        self.amountsDeliveryCharge = amountsDeliveryCharge
        self.amountsDriverTip = amountsDriverTip
    }

    init(data: [String: Any]){
        amountsDeliveryCharge = data[DatabaseRef.deliveryCharge] as? Double ?? 0.0
        amountsDriverTip = data[DatabaseRef.driverTip] as? Double ?? 0.0
    }

    static func modelToData(amount: Amount) -> [String: Any] {        
        let data : [String: Any] = [
            DatabaseRef.deliveryCharge : amount.amountsDeliveryCharge,
            DatabaseRef.driverTip : amount.amountsDriverTip
        ]
        return data
    }   
}

这是另一个结构:

struct Product {
    var price: Double
    var priceUnit: String        
    init(
        price: Double,
        priceUnit: String    
        ){
        self.price = price
        self.priceUnit = priceUnit
    }

    init(data: [String: Any]){    
        price = data[DatabaseRef.price] as? Double ?? 0.0
        priceUnit = data[DatabaseRef.priceUnit] as? String ?? ""
    }

    static func modelToData(product: Product) -> [String: Any] {            
        let data : [String: Any] = [
            DatabaseRef.price : product.price,
            DatabaseRef.priceUnit : product.priceUnit

        ]
        return data
    }
}

其他两个结构在结构上也相似。

2 个答案:

答案 0 :(得分:0)

假设它们的顺序相同,请创建具有适当属性的结构

struct Combined {
  let product: Product
  let addressInfo: AddressInfo
  let favourite: Favourite
  let amount: Amount
}

,然后使用计数器循环遍历数组以建立索引,然后使用memberWise初始化程序创建实例,或者使用静态工厂函数制作并返回数组:


static func makeFrom(products: [Product], addresses: [AddressInfo], favourites: [Favourite], amounts: [Amount]) -> [Combined] {
  guard products.count == addresses.count, addresses.count == favourites.count, favourites.count == amounts.count else {fatalError()}. //handle better in practice
  var combined = [Combined]()
  for index in 0..<products.count {
    combined.append(Combined(product: products[index],
                             addressInfo: addresses[index],
                             favourite: favourites[index],
                             amount: amounts[index])
  }
  return combined
}

上面的方法检查所有数组的长度是否相同,如果没有,则调用fatalError()。实际上,您希望比这个更好地处理错误。

然后用作:

let combined = Combined.makeFrom(products: products, addresses:addressInfo, favourites: favourites, amounts: amounts)

答案 1 :(得分:0)

您可以将数组转换为[Any]并添加它们。

let combinedVals = (products as [Any]) + (addressInfo as [Any]) + (favorites as [Any]) + (amounts as [Any])

或者,您可以创建一个协议并使您的结构遵循该协议。

protocol Combined {
}
struct Amount: Combined {
    var amountsDeliveryCharge: Double
    var amountsDriverTip: Double
}
struct Product: Combined  {
    var price: Double
    var priceUnit: String
}

let combinedValues: [Combined] = amounts + products

遍历数组时,如果需要检查数组的内容,可以使用开关来检查其类型。

combinedValues.forEach { (value) in
    switch value {
        case let value as Amount:
            print(value.amountsDriverTip)
        break
        case let value as Product:
            print(value.price)
        break
    default:
        break
    }
}