Created
June 8, 2015 05:52
-
-
Save NicholasTD07/1ac49c97a8704cc44dba to your computer and use it in GitHub Desktop.
Swift protocol as parameter for both class and struct.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Named: class { | |
var name: String { get set } | |
} | |
class Person: Named { | |
var name: String | |
init(name: String) { | |
self.name = name | |
} | |
} | |
struct Pet: Named { | |
var name: String | |
} | |
func changeName<T: Named>(inout named: T, toName newName: String) { | |
named.name = newName | |
} | |
var nick = Person(name: "nick") | |
changeName(nick, "NICK") | |
println(nick.name) // "NICK" | |
var maru = Pet(name: "Maru") | |
changeName(maru, "Hana") | |
println(maru.name) // "Hana" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment