gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:28:03 27 4
gpt4 key购买 nike

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

这是字典。

 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)

等...

如果您要查找字典,请按照以下方法使用字典:

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

关于ios - 如何访问元组字典中的值 swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51351410/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com