我想在属于非洲的世界数组中打印名称:“欧洲”
struct countries{
let name: String
let continent: String
}
var world: [countries] = [
countries(name:"japan", continent: "asia"),
countries(name:"france", continent: "europe"),
countries(name:"italy", continent: "europe"),
countries(name:"egypt", continent: "africa")
]
答案 0 :(得分:1)
world.filter { $0.continent == "europe" }.forEach { print($0.name) }
如果要使用名称数组:
world.filter { $0.continent == "europe" }.map { $0.name }