打印数组中具有特定字符串的所有元素

时间:2018-11-02 21:31:31

标签: ios arrays swift sorting

我有这个结构:

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"的元素

2 个答案:

答案 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)
    }
}