Skip to content

Instantly share code, notes, and snippets.

@satooshi
Created June 5, 2014 17:19
Show Gist options
  • Select an option

  • Save satooshi/0a25782a15dc215421b7 to your computer and use it in GitHub Desktop.

Select an option

Save satooshi/0a25782a15dc215421b7 to your computer and use it in GitHub Desktop.
hello Swift
protocol Greeter {
func greet() -> String
}
protocol エンジニャー {
func meo()
}
class Person: Greeter {
var name: String = "kitamura"
var age: Int?
init() {}
init(name: String) {
self.name = name
}
func greet() -> String {
return name
}
func hello() {
println("hello " + greet())
}
}
// extend Person
extension Person: エンジニャー {
func meo() {
println(name + " にゃーん")
}
}
// extend again
extension Person {
convenience init(name: String, age: Int) {
self.init(name: name)
self.age = age
}
}
var person = Person()
println(person.greet()) // kitamura
person.hello() // hello kitamura
// set name
person = Person(name: "hoge")
person.hello() // hello hoge
// set age
person = Person(name: "satoshi", age: 30)
person.meo() // satoshi にゃーん
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment