如何在元组swift 4的字典中访问值

时间:2018-07-15 19:12:57

标签: ios arrays swift dictionary tuples

我正在尝试访问此元组字典以获取分配给键的某些值。将数据填满后,似乎无法找到该字典。

这是字典。

 var eventAnnotation = [(eventTitle: String,
                      eventLocation: String,
                           eventLat: CLLocationDegrees,
                          eventLong: CLLocationDegrees)]()

这就是我尝试访问它的方式。

for event in eventAnnotation {
    let annotation = MKPointAnnotation()
    annotation.title = eventAnnotation.eventTitle
    annotation.subtitle = eventAnnotation.eventLocation
    annotation.coordinate = CLLocationCoordinate2D(latitude: eventAnnotation.eventLat, longitude: eventAnnotation.eventLong)
    eventMap.addAnnotation(annotation)
}

很抱歉,这很简单!

1 个答案:

答案 0 :(得分:3)

var eventAnnotation = [(eventTitle: String,
                     eventLocation: String,
                          eventLat: CLLocationDegrees,
                         eventLong: CLLocationDegrees)]()

上面不是 字典,而是一个元组数组,但是,这是其中一种,您似乎可以正确地做到这一点,其他遍历数组的方法:

1)使用Array.forEach

eventAnnotation.forEach { event in
    let annotation = MKPointAnnotation()
    annotation.title = event.eventTitle
    annotation.subtitle = event.eventLocation
    annotation.coordinate = CLLocationCoordinate2D(latitude: event.eventLat, longitude: event.eventLong)
    eventMap.addAnnotation(annotation)
}

2)使用Array.map

let annotations: [MKAnnotation] = eventAnnotation.map { event in
    let annotation = MKPointAnnotation()
    annotation.title = event.eventTitle
    annotation.subtitle = event.eventLocation
    annotation.coordinate = CLLocationCoordinate2D(latitude: event.eventLat, longitude: event.eventLong)
    annotation)
    return annotation
}
eventMap.addAnnotations(annotations)

等...

如果您要查找词典,请按照以下步骤操作字典:

// Initialize an empty dictionary
var dict: [String : (eventTitle: String, eventLocation: String, eventLat: CLLocationDegrees, eventLong: CLLocationDegrees)] = [:]

// Add an item to dictionary
dict["EventId-1"] = ("Event Title 1", "Event Location 1", 53.0, 27.0)

// Add another item to dictionary
dict["EventId-2"] = (eventTitle: "Event Title 2",
                     eventLocation: "Event Location",
                     eventLat: 53.0,
                     eventLong: 27.0)

这是您遍历字典的方式:

for (key, event) in dict {
    let annotation = MKPointAnnotation()
    annotation.title = event.eventTitle
    annotation.subtitle = event.eventLocation
    annotation.coordinate = CLLocationCoordinate2D(latitude: event.eventLat, longitude: event.eventLong)
    eventMap.addAnnotation(annotation)
}

Update-1 以下是过滤某些键的字典的方法:

1)遍历整个字典并搜索所需的键:

for (key, event) in dict {
    guard (key == "Event1" || key == "Event2") else { continue }

    let annotation = MKPointAnnotation()
    annotation.title = event.eventTitle
    annotation.subtitle = event.eventLocation
    annotation.coordinate = CLLocationCoordinate2D(latitude: event.eventLat, longitude: event.eventLong)
    eventMap.addAnnotation(annotation)
}

2)检查字典中是否存在某些键:

if let event = dict["Event1"] {
    let annotation = MKPointAnnotation()
    annotation.title = event.eventTitle
    annotation.subtitle = event.eventLocation
    annotation.coordinate = CLLocationCoordinate2D(latitude: event.eventLat, longitude: event.eventLong)
    eventMap.addAnnotation(annotation)
}