Created
March 27, 2020 18:55
-
-
Save eito/a8231fb9ed44729b30c48794e7a7c406 to your computer and use it in GitHub Desktop.
Initialization of Swift 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
import Foundation | |
class Foo: CustomStringConvertible { | |
static func calculatedValue(for thing: String) -> String { | |
return thing.uppercased() | |
} | |
func calculatedValue(for thing: String) -> String { | |
return thing.uppercased() | |
} | |
private let thing1: String | |
private let thing2: String | |
init(thing: String) { | |
thing1 = thing | |
// 'self' used in method call 'calculatedValue' before all stored properties are initialized | |
// thing2 = calculatedValue(for: self.thing1) | |
// Works, even though `self` is referenced with parameter `self.thing1` | |
thing2 = Self.calculatedValue(for: self.thing1) | |
} | |
var description: String { | |
return "\(thing1) \(thing2)" | |
} | |
} | |
let f = Foo(thing: "hello") | |
print(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment