【问题标题】:Swift - Grouping and Ordering List ItemsSwift - 分组和排序列表项
【发布时间】:2018-03-25 11:49:36
【问题描述】:

开发人员。我有一个商店,我需要在其中打印按商品类别分组的每个客户购买清单。出于分组和排序的目的,请考虑以下代码:

protocol GroceryItem{
    func cost () -> Float
    func description() -> String
    func category() -> String

}

  struct GroceryStore {

    func printReceit(customer: [Customer])  {

        customer.forEach { (client) in
            print("---------------------------------------------------")
            print("     Printing Receipt for Customer: \(client.name)")
            var total: Float = 0
            print("---------------------------------------------------")
            client.groceryList.forEach({ (item) in
                if let groceryItem = item as? GroceryItem {
                    print("******** \(groceryItem.category())")
                    print("                 \(groceryItem.description())")
                    total += groceryItem.cost()
                }
            })
            print("Total: \(total)")
            print("---------------------------------------------------")
        }

    }
}

enum Category: String {
    case Fruits
    case Vegetables
    case Dairies
    case Meats
    case Fishes
    case Drinks
    case Others
}

enum Unit: String {
    case Kg
    case Lt
    case Un
}

struct Customer{
    let name: String
    let groceryList: [Any]
}

struct Product: GroceryItem {

    let categ: Category
    let name: String
    let price: Float
    let qty: Float
    let unit: Unit

    func cost() -> Float {
        return price
    }
    func description() -> String {
        return "\(name): \(price)"
    }
    func category() -> String {
        return categ.rawValue
    }
}


// Store
let Walmart = GroceryStore()

// Store Products
let strawberry = Product(categ: .Fruits, name: "Strawberry", price: 1.75, qty: 1, unit: .Kg)
let apple = Product(categ: .Fruits, name: "Apple", price: 100, qty: 1.45, unit: .Un)
let pear = Product(categ: .Fruits, name: "Pear", price: 120, qty: 1.50, unit: .Un)
let hamburguer = Product(categ: .Meats, name: "Hamburguer", price: 750, qty: 1.5, unit: .Kg)
let parmesan = Product(categ: .Dairies, name: "Parmesan", price: 1000, qty: 2, unit: .Kg)
let milk = Product(categ: .Dairies, name: "Milk", price: 150, qty: 2, unit: .Lt)
let yogurt = Product(categ: .Dairies, name: "Danone", price: 300, qty: 1, unit: .Un)
let yogurt2 = Product(categ: .Dairies, name: "Oikos", price: 1000, qty: 2, unit: .Un)
let drink = Product(categ: .Drinks, name: "Fanta", price: 600, qty: 1.5, unit: .Lt)
let meat = Product(categ: .Meats, name: "Angus", price: 4000, qty: 5.0, unit: .Kg)
let drink2 = Product(categ: .Drinks, name: "Compal", price: 350, qty: 1, unit: .Un)

// Customers
let yasmin = Customer(name: "Yasmin", groceryList: [apple, hamburguer, milk, yogurt, drink])
let camila = Customer(name: "Camila", groceryList: [pear, parmesan, milk, yogurt2, meat, drink2])

Walmart.printReceit(customer: [yasmin, camila])

哪个输出这个:

/*
---------------------------------------------------
     Printing Receipt for Customer: Yasmin
---------------------------------------------------
******** Fruits
                 Apple: 100.0
******** Meats
                 Hamburguer: 750.0
******** Dairies
                 Milk: 150.0
******** Dairies
                 Danone: 300.0
******** Drinks
                 Fanta: 600.0
Total: 1900.0
---------------------------------------------------
---------------------------------------------------
     Printing Receipt for Customer: Camila
---------------------------------------------------
******** Fruits
                 Pear: 120.0
******** Dairies
                 Parmesan: 1000.0
******** Dairies
                 Milk: 150.0
******** Dairies
                 Oikos: 1000.0
******** Meats
                 Angus: 4000.0
******** Drinks
                 Compal: 350.0
Total: 6620.0
---------------------------------------------------
*/

但所需的输出应该是这样的,即:按字母顺序排列并按类别分组的产品。我怎样才能做到这一点?:

/*
---------------------------------------------------
     Printing Receipt for Customer: Camila
---------------------------------------------------
******** Dairies ********************************
                Milk: 150.0
                Oikos: 1000.0
                Parmesan: 1000.0

******** Drinks ********************************
                Compal: 350.0

******** Fruits ********************************
                Pear: 120.0

******** Meats ********************************
                Angus: 4000.0

Total: 6620.0
---------------------------------------------------
*/

【问题讨论】:

    标签: ios arrays swift dictionary predicate


    【解决方案1】:

    你可以找到很多方法来实现它。一种方法是创建一个分组Dictionary

    struct GroceryStore {
    
        func printReceit(customer: [Customer])  {
    
            customer.forEach { (client) in
                print("---------------------------------------------------")
                print("     Printing Receipt for Customer: \(client.name)")
                var total: Float = 0
                print("---------------------------------------------------")
                //Creating a grouped Dictionary
                let groceryItemsByCategory = Dictionary(grouping: client.groceryList, by: {$0.category()})
                groceryItemsByCategory.sorted{$0.key < $1.key}.forEach {entry in
                    let (key, groceryItems) = entry
                    print("******** \(key) ********************************")
                    groceryItems.forEach {groceryItem in
                        print("                 \(groceryItem.description())")
                        total += groceryItem.cost()
                    }
                }
                print("Total: \(total)")
                print("---------------------------------------------------")
            }
    
        }
    }
    

    我对您的代码进行了一些修改以简化:

    struct Customer{
        let name: String
        let groceryList: [GroceryItem]
    }
    

    如果此更改不符合您的要求,您可能需要进行更多类型转换或类型检查。


    我保留了所有其他部分,但最好保持 Swift 中的简单编码规则:只有类型名称以大写字母开头。

    【讨论】:

    • 非常感谢,@OOPer。您刚刚拯救了这一天!
    • 非常好的答案!谢谢@OOPer
    猜你喜欢
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多