我有这个结构:
struct MapsData {
var company = String()
var city = String()
var mark = String()
var latitude: Double
var longitude: Double
}
var places = [
MapsData(company: "Tireshop", city: "Berlin", mark: "market", latitude: 37.33233141, longitude: -122.03121860),
MapsData(company: "Ginza", city: "Tokio", mark: "restaurant", latitude: 37.331831, longitude: -122.031862),
MapsData(company: "McOz", city: "London", mark: "restaurant", latitude: 37.331831, longitude: -122.031862)
]
如何打印所有具有mark: "restaurant"
的元素
答案 0 :(得分:4)
您可以使用过滤器来获取mark
设置为“餐厅”的所有元素
print(places.filter({$0.mark == "restaurant"}))
答案 1 :(得分:2)
for el in places {
if el.mark == "restaurant" {
print(el.city, el.company, el.latitude, el.longitude, el.mark)
}
}