Created
June 11, 2014 18:12
-
-
Save edom18/f4ff6805a9ca97267624 to your computer and use it in GitHub Desktop.
[Swift] convenienceイニシャライザとdesignated(指定)イニシャライザ ref: http://qiita.com/edo_m18/items/733d8c81fb00942e3167
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
class SubClass: SomeClass2 { | |
init() { | |
self.name = "name" // `super.init`前に、Super classの変数にはアクセスできない | |
super.init() // `convenience initializer`はSub classから直接呼べない | |
} | |
} |
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
class SubClass: SomeClass2 { | |
var hoge: String | |
init() { | |
self.hoge = "foo" // Sub classで定義されている変数には`super.init`前にアクセス可能 | |
super.init(name: "hogehoge") // Super classのdesignated initializerは`init(name:)` | |
self.name = "name" // Super classのinitializerを呼んだ後ならアクセスできる | |
} | |
} |
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
class SomeClass { | |
var name: String | |
init(name: String) { | |
self.name = name | |
} | |
convenience init() { | |
self.init(name: "default name") | |
} | |
} | |
var aClass: SomeClass = SomeClass() // => "name: default name" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment