Protocol extensions constraints in Swift
Forums:
protocol PText {
func asText() -> String
}
struct Row: PText {
var id: Int
var name: String
func asText() -> String {
return "\(id) \(name)"
}
}
extension CollectionType where Generator.Element : PText {
func asStrings() -> [String] {
return map({$0.asText()})
}
}
class func example() {
let rows = [
Row(id: 1, name: "one"),
Row(id: 2, name: "two")]
let strings = rows.asStrings()
print(strings)
}
- Log in to post comments