Skip to content

Instantly share code, notes, and snippets.

@stansidel
Created June 26, 2019 06:23
Show Gist options
  • Save stansidel/938093b99fa0a888cc430efdf83742b0 to your computer and use it in GitHub Desktop.
Save stansidel/938093b99fa0a888cc430efdf83742b0 to your computer and use it in GitHub Desktop.
import Foundation
class MyClass {
private let name: String
init(name: String) {
self.name = name
}
// Pure computed property, cannot be set. Behaves like a constant field for the user of the class
var computed: String {
return "My name is \(name)"
}
// Property with getter only. Much like a computed property, defined with another syntax
var gettable: String {
get {
return "Gettable from name \(name)"
}
}
// Property with both getter and setter. Usually works as a proxy to other object's property. Or to control what's being set
private var _prop: String?
var prop: String {
get { return _prop ?? "" }
set { _prop = newValue }
}
}
@dohoan259
Copy link

Good gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment