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

David Sweeris davesweeris at mac.com
Fri Apr 29 10:52:30 CDT 2016

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.

- Dave Sweeris

> On Apr 29, 2016, at 9:52 AM, Bob Davidson via swift-users <swift-users at 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 at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

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

More information about the swift-users mailing list