【问题标题】:How to Get key value pair tuple array in swift?如何快速获取键值对元组数组?
【发布时间】:2017-02-23 02:08:45
【问题描述】:

我有对元组数组 pickerDataVisitLocation.just 我想知道如何使用 uniqId ex 204 从我的数组中返回键值对位置

var pickerDataVisitLocation:[(uniqId:Int,location:String)] = [(203,"Home"),(204,"Hospital"),(205,"Other")]
var selectedIndex = pickerDataVisitLocation[1].uniqId
pickerDataVisitLocation[selectedIndex].location //<--fatal error: Index out of range

【问题讨论】:

  • 所以 selectedIndex 是 204,因为这是数组中第 1 项的 uniqId。然后你尝试获取pickerDataVisitLocation[204],但它只有 3 个项目。有什么问题?你希望它做什么?
  • 是否需要元组?我只想做一本[Int:String]的字典
  • 我想要的是获得 uniqId 为 204 的对值位置“医院”
  • selectedIndex 将是您根据此代码给出的,即 203,204,205,并且您的数组只有 3 个元素

标签: ios arrays swift tuples key-pair


【解决方案1】:

根据给定的代码:
试试这个

var pickerDataVisitLocation:[(uniqId:Int,location:String)] = [(203,"Home"),(204,"Hospital"),(205,"Other")]
let selectedIndex = pickerDataVisitLocation[1].uniqId
var location = ""

for item in pickerDataVisitLocation {
    if item.uniqId == selectedIndex {
        location = item.location
    }
}

print(location) //Will print Hospital here

【讨论】:

    【解决方案2】:

    利用Sequencefirst(where:)方法

    您可以利用Sequencefirst(where:) 来访问满足基于元组元素的第一个成员(uniqId)的布尔要求的数组的第一个元组元素。对于生成的元组元素,只需访问元组的第二个成员 (location)。

    var pickerDataVisitLocation: [(uniqId: Int, location: String)] = 
        [(203, "Home"), (204, "Hospital"), (205, "Other")]
    
    // say for a given uniqId 204
    let givenId = 204
    let location = pickerDataVisitLocation
                   .first{ $0.uniqId == givenId }?.location ?? ""
    print(location) // Hospital
    

    如果找不到给定 id 的元组元素,则上述方法将导致结果字符串为空(由于 nil 合并运算符)。作为替代方案,您可以使用可选的绑定子句仅对来自.first 的非nil 返回进行处理:

    var pickerDataVisitLocation: [(uniqId:Int,location:String)] = 
        [(203,"Home"),(204,"Hospital"),(205,"Other")]
    
    // say for a given uniqId 204
    let givenId = 204
    if let location = pickerDataVisitLocation
                      .first(where: { $0.uniqId == givenId })?.location {
        print(location) // Hospital
    }
    

    可能的替代方法:考虑使用字典

    最后,由于元组元素的第一个成员 uniqId 暗示了唯一成员,并且其类型 IntHashable,您可能需要考虑使用字典而不是元组数组.这将简化与给定唯一 Int id 关联的值的访问,但您将失去字典中“元素”(键值对)的顺序,因为字典是无序的集合。

    var pickerDataVisitLocation = [203: "Home", 204: "Hospital", 205: "Other"]
    
    // say for a given uniqId 204
    let givenId = 204
    if let location = pickerDataVisitLocation[givenId] {
        print(location) // Hospital
    }
    

    【讨论】:

    • 我希望我能做更多的UpVote
    • @NinjaDeveloper 乐于提供帮助 :)
    • 嘿@NinjaDeveloper,这已经有好几年了。我是 swift 新手,如何从结构中访问更多值,而不仅仅是一个“?.location”,如果我希望所有值都对应。
    【解决方案3】:

    您可以尝试以下方法。

    extension Array {
        func tupleWithId(id: Int) -> (uniqId:Int,location:String)? {
            let filteredElements = self.filter { (tuple) -> Bool in
                if let tuple = tuple as? (uniqId:Int,location:String) {
                    return tuple.uniqId == id
                }
                return false
            }
    
            if filteredElements.count > 0 {
                let element = filteredElements[0] as! (uniqId:Int,location:String)
                return element
            }
            return nil
        }
    }
    var pickerDataVisitLocation:[(uniqId:Int,location:String)] = [(203,"Home"),(204,"Hospital"),(205,"Other")]
    var selectedIndex = pickerDataVisitLocation[1].uniqId
    pickerDataVisitLocation.tupleWithId(id: selectedIndex)?.location
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      相关资源
      最近更新 更多