[swift-users] Stored Property, Externally Read-Only

Bob Davidson thegreatvoid at gmail.com
Fri Apr 29 09:52:13 CDT 2016


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

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?

Bob Davidson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160429/923019b9/attachment.html>

More information about the swift-users mailing list