sguruapp@gmail.com
facebook vk instagram Телеграм youtube

Пример работы с фильтром и структурами (filter structure) - Swift 5


struct StructShop {
    var title: String
    var price: Double
    var colors: [String]
}

var shopArray : [StructShop] = [
    StructShop(title: "Sneakers", price: 199, colors: ["Red", "Green", "Black"]),
    StructShop(title: "T-shirts", price: 32, colors: ["White", "red"]),
    StructShop(title: "Socks", price: 17.90, colors: ["Blue", "Green"]),
    StructShop(title: "Jeans", price: 199, colors: ["Black", "White"]),
    StructShop(title: "Shoes", price: 199, colors: ["Red", "Gray"]),
    StructShop(title: "Short", price: 199, colors: ["Pink", "DarkGray"])
]


// 1
func one(color: String) {
    var newShop = [StructShop]()
    var maxColor = 0
    for goods in shopArray {
        maxColor = goods.colors.count
        for allCount in 0..<maxColor {
            if goods.colors[allCount].lowercased() == color.lowercased() {
                print("Goods", goods.title, goods.colors[allCount])
            }
        }
        
    }
}


one(color: "RED")



// 2
func two(color: String) {
    let new = shopArray.filter {$0.colors.contains(where: {$0.lowercased() == color.lowercased()}) == true}
}

two(color: "RED")


Возврат к списку

обучись профессии будущего

Хочешь стать успешным разработчиком
приложений? Тогда добро пожаловать к нам.

обучись профессии будущего