I have an array of Tag objects that have a property called tag, which is a string.
public struct Tag {
public let name: String
}
I would like to get an array of all of these name properties.
Given an array of tags, in Objective-C I would accomplish this with this line:
NSArray *tagNames = [tags valueForKey:@"name"]
How can I achieve the same thing in Swift?
I've tried:
let tagNames = tags.map({ $0.name })
But get a compiler error: "Value of type '[Tag]' has no member 'name'.
It looks like you have an array which contains another array of Tag objects. This works for me:
let tags = [Tag(name: "tag1"), Tag(name: "tag2"), Tag(name: "tag3")]
let names = tags.map{$0.name }
print("Names: \(names)")