Last active
April 1, 2021 15:15
-
-
Save 0xLeif/e27a4ac1568ed786a111517e6d2f85fe to your computer and use it in GitHub Desktop.
Swift Property Requirements
This file contains hidden or 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
/** | |
[Swift Protocols](https://docs.swift.org/swift-book/LanguageGuide/Protocols.html#ID269) | |
# Property Requirements | |
A protocol can require any conforming type to provide an instance property or | |
type property with a particular name and type. The protocol doesn’t specify | |
whether the property should be a stored property or a computed property it | |
only specifies the required property name and type. The protocol also specifies | |
whether each property must be gettable or gettable and settable. | |
If a protocol requires a property to be gettable and settable, that property | |
requirement can’t be fulfilled by a constant stored property or a read-only | |
computed property. If the protocol only requires a property to be gettable, | |
the requirement can be satisfied by any kind of property, and it’s valid for | |
the property to be also settable if this is useful for your own code. | |
Property requirements are always declared as variable properties, prefixed with | |
the var keyword. Gettable and settable properties are indicated by writing | |
{ get set } after their type declaration, and gettable properties are | |
indicated by writing { get }. | |
*/ | |
protocol GetSetProtocol { | |
var email: String { get set } | |
var username: String { get set } | |
var firstName: String? { get set } | |
var lastName: String? { get set } | |
} | |
protocol GetProtocol { | |
var email: String { get } | |
var username: String { get } | |
var firstName: String? { get } | |
var lastName: String? { get } | |
} | |
// Must be `var` or `{ get set }` | |
// Can not be `let` or `{ T }` | |
struct User: GetSetProtocol { | |
private var hiddenEmailVariable: String = "" | |
var email: String { | |
set { | |
print(newValue) | |
hiddenEmailVariable = newValue | |
} | |
get { | |
return hiddenEmailVariable | |
} | |
} | |
var username: String { | |
didSet { | |
print(username) | |
} | |
} | |
var firstName: String? | |
var lastName: String? | |
init(email: String, username: String, | |
firstName: String?, lastName: String?) { | |
self.username = username | |
self.firstName = firstName | |
self.lastName = lastName | |
// set `hiddenEmailVariable` to the value of `email` | |
self.email = email | |
} | |
} | |
// Can be any: `{ }`, `let`, `var` | |
struct UserResponse: GetProtocol { | |
private var hiddenEmailVariable: String = "" | |
var email: String { | |
set { | |
print(newValue) | |
hiddenEmailVariable = newValue | |
} | |
get { | |
return hiddenEmailVariable | |
} | |
} | |
var username: String { | |
didSet { | |
print(username) | |
} | |
} | |
let firstName: String? | |
var lastName: String? { | |
nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment