Skip to content

Instantly share code, notes, and snippets.

@mchirico
Created September 21, 2014 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mchirico/8698f3e05015d1374f59 to your computer and use it in GitHub Desktop.
Save mchirico/8698f3e05015d1374f59 to your computer and use it in GitHub Desktop.
Unsolved Swift Problem ... can't figure this out...
/*
someObjects is [AnyObject!], but I want to
define it again as Array of Dictionary<String:String>
*/
// Starts out like this
let someObjects: [AnyObject!] = [
["a one":"1", "a two":"2","a three":"3"],
["b one":"1", "b two":"2","b three":"3"]]
// Now how do you get it back.
// The following are all failures
func findAllKV(array: [[String:AnyObject]], key: String, value: AnyObject) -> [[String:AnyObject]] {
var all : [[String:AnyObject]] = []
for dict in array {
if let val: AnyObject = dict[key] {
if val === value {
all.append(dict)
}
}
}
return all
}
func makeDict<T: Hashable>(arr: [T]) {
let x = Dictionary<T, Int>()
}
for i in someObjects {
makeDict(i!)
}
if let k=someObjects as? [[AnyObject:AnyObject?]!] {
}
if let k=someObjects as? [Dictionary?]{
print("yes")
} else {
println("nope")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment