๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“Œ Language/Swift

[Swift] keyword๋ฅผ ์ด์šฉํ•ด์„œ ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฒ•

by Fomagran ๐Ÿ’ป 2019. 12. 5.
728x90
๋ฐ˜์‘ํ˜•

์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ์ฒ˜์Œ ์•Œ์•˜๋‹ค.

let recipes = ["Put eggs in a frying pan", "Cut the beef", "Boil the beef"]
let keyword = "beef"

func search(recipes:[String],keyword:String) -> [String]{
    var foundRecipes = [String]()
    for recipe in recipes{
        if recipe.contains(keyword) == true {
            foundRecipes.append(recipe)
        }
        
    }
    return foundRecipes
}

var a = search(recipes: recipes, keyword: "egg")

๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ ํ•ต์‹ฌ์€ contains์™€ append๋ฅผ ์•Œ๊ณ ์žˆ๋Š๋ƒ ์—†๋Š๋ƒ ๊ฐ™๋‹ค!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€