格式化数组,如购物清单

时间:2016-12-30 22:34:35

标签: arrays swift swift3 shopping-cart

我有一个格式如下的数组:

["Trousers : 15.50", "Trousers : 15.50", "Jumper : 12.99", "Shoes: 50.00"]

我想像这样格式化:

["2x Trousers : 31.00", "1x Jumper : 12.99", "1x Shoes: 50.00"]

我尝试使用此格式化:

 var counts: [String:Int] = [:]
 var shoppingList = ["Trousers : 15.50", "Trousers : 15.50", "Jumper : 12.99", "Shoes: 50.00"]
 var formattedShoppingList = [String]()

     for item in shoppingList {
         counts[item] = (counts[item] ?? 0) + 1
     }


     for (key, value) in counts {

         let display:String = String(value) + "x " + key
         formattedShoppingList.append(display)

     }

但我得到了这个

["2x Trousers : 15.50", "1x Jumper : 12.99", "1x Shoes: 50.00"]

如果我使用字典,我就不能复制。我该如何处理?

3 个答案:

答案 0 :(得分:1)

我会创建一个结构来表示项目名称/价格对(以及将来可能的其他数据,例如库存中的数量)。

struct Item: Hashable {
    let name: String
    let price: Double

    public var hashValue: Int { return name.hashValue ^ price.hashValue }

    public static func ==(lhs: Item, rhs: Item) -> Bool {
        return lhs.name == rhs.name && rhs.price == rhs.price
    }
}

let shoppingList = [
    Item(name: "Trousers", price: 15.50),
    Item(name: "Trousers", price: 15.50),
    Item(name: "Jumper", price: 12.99),
    Item(name: "Shoes", price: 50),
]

let counts = shoppingList.reduce([Item: Int]()){counts, item in
    var counts = counts
    counts[item] = (counts[item] ?? 0) + 1
    return counts
}

let formattedShoppingList = counts.map{ item, count in "\(count)x \(item.name): £\(item.price)" }

print(formattedShoppingList)
  

[“2x Trousers:15.5英镑”,“1x鞋子:50.0英镑”,“1x跳线:12.99英镑”]

答案 1 :(得分:1)

您可以使用此结构,它接受您的字符串作为构造函数的参数:

struct ShoppingItem: Hashable {
    let name: String
    let price: NSDecimalNumber

    //Expects a String in the form "ElementName : Price"
    init?(description: String) {
        let splitDescription = description.components(separatedBy: ":")

        guard splitDescription.count == 2 else { return nil }

        name = splitDescription[0].trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        price = NSDecimalNumber(string: splitDescription[1])
    }

    public var hashValue: Int {
        return "\(name)\(price)".hashValue
    }
}
func ==(lhs: ShoppingItem, rhs: ShoppingItem) -> Bool {
    return lhs.hashValue == rhs.hashValue
}

有了它,您可以将购物清单转换为这样的购物项目列表(考虑到这会丢弃它无法转换的项目,您可以检查零项目以确保所有项目都可以转换) :

var shoppingItems = shoppingList.flatMap(ShoppingItem.init(description:))

然后,你只需要做你以前做过的事情,只将价格乘以最后:

var counts = [ShoppingItem: Int]()
for item in shoppingItems {
    counts[item] = (counts[item] ?? 0) + 1
}

for (key, value) in counts {
    let multipliedPrice = key.price.multiplying(by: NSDecimalNumber(value: value))
    let display = "\(value)x \(key.name) : \(multipliedPrice)"
    formattedShoppingList.append(display)
}

答案 2 :(得分:-1)

你不需要一个简单的结构或类;使用元组数组:

    var shoppingList = [("Trousers", 15.50), ("Trousers", 15.50), ("Jumper", 12.99), ("Shoes", 50.00)]

    for (name, price) in shoppingList {
        print(name, price)
    }