合并 UnitMass 和 UnitLength 数组

Combining UnitMass and UnitLength arrays

我正在尝试组合两个测量阵列:

var unitMasses: [UnitMass] {
    return [.milligrams, .grams, .kilograms, .ounces, .pounds]
}

var unitLengths: [UnitLength] {
    return [.centimeters, .decimeters, .meters]
}

合二为一:

var units: [AnyObject] {
    // This works:
    return [unitMasses].flatMap{[=11=]}
    // But I've tried the following and this doesn't:
    //return ([unitMasses as AnyObject] + [unitVolumes as AnyObject]).flatMap{[=11=]}
}

我希望能够访问 units 变量中元素的 .symbol 属性:

var symbols: [String] {
    return units.map({ unit in unit.symbol })
}

谢谢。

从我的角度来看这种方式比较直接forward.I之前没有看到任何需要合并数组的地方。

var symbols = unitMasses.map({[=10=].symbol})
symbols += unitLengths.map({[=10=].symbol})

print(symbols) // ["mg", "g", "kg", "oz", "lb", "cm", "dm", "m"]

接受的答案效果很好。为了完整起见,我是这样使用它的:

var units: [Dimension] {
    return unitMasses.map({ unit in unit }) + unitLengths.map({ unit in unit })
}

var symbols: [String] {
    return units.map({ unit in unit.symbol })
}