Skip to content

Instantly share code, notes, and snippets.

@eito
Created March 27, 2020 18:55
Show Gist options
  • Save eito/a8231fb9ed44729b30c48794e7a7c406 to your computer and use it in GitHub Desktop.
Save eito/a8231fb9ed44729b30c48794e7a7c406 to your computer and use it in GitHub Desktop.
Initialization of Swift class
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