Stored Property, Externally Read-Only


(Bob Davidson) #1

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


(Nate Birkholz) #2

That's correct.

···

On Fri, Apr 29, 2016 at 8:52 AM, David Sweeris via swift-users < swift-users@swift.org> wrote:

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

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Nate Birkholz


(David Sweeris) #3

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


(Bob Davidson) #4

Great, thanks guys

···

On Fri, Apr 29, 2016 at 12:00 PM Nate Birkholz <nbirkholz@gmail.com> wrote:

That's correct.

On Fri, Apr 29, 2016 at 8:52 AM, David Sweeris via swift-users < > swift-users@swift.org> wrote:

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

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

--
Nate Birkholz