Protocols collection in Swift
Forums:
let rows: [Named] = [
Flower(name: "Rose"),
City(name: "Rome"),
Star(name: "Sirius")]
let list = rows.map({$0.name}).joinWithSeparator(", ")
//list is Rose, Rome, Sirius
protocol Named {
var name: String {get set}
}
struct Flower: Named {
var name: String
}
struct City: Named {
var name: String
}
struct Star: Named {
var name: String
}
- Log in to post comments