ios - 如何快速访问元组字典中的值 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)
}
对不起,如果这很简单!
解决方案
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)
}
推荐阅读
- python - 尝试生成 2exp(40) 矩阵时出现内存问题
- php - 如何避免在选择标签中自动选择最后一个选项?
- java - 从我的数据库中获取信息并将它们写入 JTable。错误:java.lang.IndexOutOfBoundsException:索引:2,大小:2
- c++ - 运算符函数中的第二个 const 做什么?
- java - 如何获取 ArrayList 中的最后一个“示例”项?
- java - 如何在 Java 中使用 BigDecimal 获得正弦值的准确结果?
- python - 导入未在函数中定义
- r - 虹膜数据集均值()按物种
- marklogic - 在 MarkLogic 中从 Schematron 调用外部 API 和库函数
- javascript - 如何合并 2 个 js 对象以形成没有子级别的 json 结构?