Saturday, September 24, 2022
HomeiOS Developmentios - What's one of the best ways to implement the operate(String)...

ios – What’s one of the best ways to implement the operate(String) -> Set?


You will need to implement a operate that meets the next necessities:

  • Returns: 10 product names containing the desired string.
    If there are a number of merchandise with the identical identify, producer’s identify is added to product’s identify within the format ” – “,
    in any other case returns merely “”.
    */

func listProductsByName(searchString: String) -> Set<String>

You can’t use error dealing with, so I assumed to fill the Set with default knowledge, however this resolution doesn’t work. What’s one of the best ways to implement this operate?

func listProductsByName(searchString: String) -> Set<String> {
    
    var searchProducts = Set<String>()
    
    for product in merchandise {
        if product.identify.comprises(searchString) {
            if !searchProducts.comprises(product.identify){
                searchProducts.insert(product.identify)}
            else { searchProducts.insert(product.identify + " - " + product.producer)}
            }
        if searchProducts.rely == 10 {
            break
        }
    }
    
    if searchProducts.rely < 10 {
        var emptyElement = 0
        print("Найдено меньше 10 товаров: (searchProducts)")
        repeat {
            searchProducts.insert("Пустой элемент № (emptyElement)")
            emptyElement += 1
        } whereas searchProducts.rely < 10
    }

    return searchProducts
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular