Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write array.valueForKey in Swift?

What's the most convenient code to write

[results valueForKey:@"attribute"]

in swift? I've found here that it's recommended to use the map() function with a closure:

swiftarray.map({$0["attribute"]})

N.B. swiftarray is not a NSArray

like image 848
aneuryzm Avatar asked Jan 06 '23 09:01

aneuryzm


1 Answers

I'am not sure but if you have some object in array other than Dictionary (or other type that supports subscripting), you will need to use following code

results.map({$0.attribute})
like image 77
Peter K Avatar answered Jan 16 '23 01:01

Peter K