首页 > 解决方案 > 如何快速访问元组字典中的值 4

问题描述

我正在尝试访问此元组字典以获取分配给键的某些值。一旦我用数据填充字典,我似乎无法找到如何访问字典。

这是字典。

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

对不起,如果这很简单!

标签: iosarraysswiftdictionarytuples

解决方案


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)

ETC...

如果您正在查找字典,以下是您对字典执行相同操作的方法:

// 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)
}

推荐阅读