【问题标题】:How to acces a value in a dictionary of tuples swift 4如何访问元组字典中的值 swift 4
【发布时间】:2018-07-15 19:12:57
【问题描述】:

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

这是字典。

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

对不起,如果这很简单!

【问题讨论】:

  • 这不是字典。它是一个元组数组。所以你必须通过索引或搜索来访问它。

标签: ios arrays swift dictionary tuples


【解决方案1】:
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)
}

【讨论】:

  • 您好阿米尔,感谢您的快速回复。我不断收到此错误“类型的值”[(eventTitle:String,eventLocation:String,eventLat:CLLocationDegrees,eventLong:CLLocationDegrees)]'(又名'Array ') 没有成员 'eventTitle' ' 有什么想法吗??
  • 我贴了几个例子,你用的是哪一个?或者只是编辑您的问题并在您遇到错误的地方添加其他代码
  • 我正在使用 'array.ForEach' 我收到上述代码行的错误 - annotation.title = event.eventTitle
  • 如果您正在搜索某些键,请查看我的示例 var dict: 和上面的 Update-1
  • 已修复!!!我在从firebase填充数组之前调用添加注释。谢谢!!!
猜你喜欢
  • 1970-01-01
  • 2022-01-22
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多