Hello,
Does Swift have a syntax for allowing a stored property to be internally
changed by a class/structure, but external access is read-only?
For example, my class/structure may have a date property such as
“lastUpdated”. Outside code should have access to read the “lastUpdated”
property, but should not be allowed to change it. Periodically, my class
may perform an “update” and would internally change the value of
“lastUpdated”.
The only way I see to support this with a private property and a computed,
get-only property:
private var internalLastUpdated: NSDate?
var lastUpdated: NSDate? { return internalLastUpdated }
This there a better way?
Thanks,
Bob Davidson
Yep, declare it like this:
public private(set) var lastUpdated: NSDate?
At least I’m pretty sure that’s the syntax. I don’t have Xcode in front of me to double-check.
HTH
- Dave Sweeris
···
On Apr 29, 2016, at 9:52 AM, Bob Davidson via swift-users <swift-users@swift.org> wrote:
Hello,
Does Swift have a syntax for allowing a stored property to be internally changed by a class/structure, but external access is read-only?
For example, my class/structure may have a date property such as “lastUpdated”. Outside code should have access to read the “lastUpdated” property, but should not be allowed to change it. Periodically, my class may perform an “update” and would internally change the value of “lastUpdated”.
The only way I see to support this with a private property and a computed, get-only property:
private var internalLastUpdated: NSDate?
var lastUpdated: NSDate? { return internalLastUpdated }
This there a better way?
Thanks,
Bob Davidson
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users