I created this func
static func getCategories() -> [QCategoryy] {
let list:[QCategoryy] = [QCategoryy(name: "bar", image: UIImage(named: "food box.png")!), QCategoryy(name :"night_club", image: UIImage(named: "accessories box.png")!), QCategoryy(name: "gym", image: UIImage(named: "jewellery box.png")!), QCategoryy(name: "spa", image: UIImage(named: "beauty box.png")!), QCategoryy(name: "museum", image: UIImage(named: "history box.png")!)]
return list
}
to have a list of different categories taken from google places API, https://developers.google.com/places/supported_types, but what i would like to do is grouping more categories into one like
let list:[QCategoryy] = [QCategoryy(name: "bar, restaurant, cafè", image: UIImage(named: "food box.png")!)
how can i do something like that? I mean how i have to write it to make it work?
答案 0 :(得分:0)
You can make a helper function to simplify that code. Something like this:
struct Category {
let name: String
let image: UIImage?
}
func categories(named names: [String], image: UIImage? = UIImage(named: "image")) -> [Category] {
return names.map() { name in
return Category(name: name, image: image)
}
}
You can then call it like this:
let list = categories(named: ["a","b","c"]) //Uses default image value specified in the function declaration
let secondList = categories(named: ["a","b","c"], image: UIImage(named: "food box.png"))
Obviously instead of using that Category struct, you'd use the QCategory type you need.
If image isn't an optional property of QCategory, you might need to make that parameter required by categories(_:,_:) or guard against it in the function body.