2

How do I loop through a dictionary and check for each items value type?

Example dictionary:

var dict = ([
    "a number" : 1,
    "a boolean" : false,
    "a string" : "Hello"
])

My attempts at checking for values:

for item in dict.1 {
    if let current = item as? NSNumber {
        print(current)
    }
}

and I tried this:

for item in settings {
    if item[1] == item[1] as NSNumber {
        print(item)
    }
}

Important note:

As @Leo pointed out, it's important to keep track of what your dictionary's value type is while comparing. The dictionary that I included as my example would be of type [String : NSObject] and the dictionary in my project was of type [String : NSNumber]. They have to be approached differently!

4

4 回答 4

5

您可以is快速使用

var dict = ([
    "a number" : 1,
    "a boolean" : false,
    "a string" : "Hello"
])

for (key,value) in dict{
    if value is String{
        println(value)
    }
}

你也可以得到className

for (key,value) in dict{
   println(String.fromCString(object_getClassName(value)))

}

这将记录

Optional("__NSCFString")
Optional("__NSCFNumber")
Optional("__NSCFBoolean")

更新:

Swift 是一种类型安全的语言,所以如果你有这样的字典

var dict = ([
        "a number" : 1,
        "a boolean" : false,
        ])

dict 是 的类型[String : NSNumber],所以你不需要检查。

但是,如果你有这样的字典

 var dict = ([
        "a number" : 1,
        "a boolean" : false,
        "a string" : "Hello"
        ])

然后 dict 是类型[String:NSObject],在这种情况下你需要检查类型。

更新,检查它是 Int 还是 Bool

最好将您的字典声明为[String:Any] Then

      var dict:[String:Any] = ([
        "a number" : 1,
        "a boolean" : false,
        ])

    for (key,value) in dict{
        if value is Bool{
            println(value)
       }
        if value is Int{
            println(value)
        }
    }
于 2015-06-11T01:25:33.460 回答
1

从快速参考,这里是如何迭代字典

遍历字典

您可以使用 for-in 循环遍历字典中的键值对。字典中的每一项都作为 (key, value) 元组返回,您可以将元组的成员分解为临时常量或变量作为迭代的一部分:

for (airportCode, airportName) in airports {
        println("\(airportCode): \(airportName)")
    }
    // YYZ: Toronto Pearson
    // LHR: London Heathrow

所以这应该适用于你的例子。

var dict = ([
  "a number" : 1,
  "a boolean" : false,
  "a string" : "Hello"
  ])


for (k,v) in dict {
  if let x = v as? NSNumber {
    println("x = \(x)")
  }
}
于 2015-06-11T01:25:04.360 回答
1

您还可以使用 dynamicType 查找类,然后使用简单的 switch 语句采取适当的操作

var dict = ([
  "a number" : 1,
  "a boolean" : false,
  "astring" : "Hello"
  ])

for (k,v) in dict {
  switch "\(v.dynamicType)" {
    case "__NSCFString":
      println("found String = \(v)")
    case "__NSCFBoolean":
      println("found Boolean = \(v as! Bool)")
    case "__NSCFNumber":
      println("found Number = \(v)")
    default:break
  }
}
于 2015-06-11T03:26:12.343 回答
0

对于那些遇到警告问题的人,请确保按照上述@Leo 检查您的类型。如果你的值类型是 NSNumber,你会得到错误。如果它是 NSObject 类型,请使用@Leo 或@nPn 的答案。

在此处输入图像描述

这在我的项目中对我有用,它的值类型为 NSNumber:

for (key, value) in settings {

    if value == value as Bool {
        print(value)
    }
    if value == value as NSNumber {
        print(value)
    }
}
于 2015-06-11T01:43:31.373 回答