Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the use of `set` keyword during property declaration in Swift?

I saw this line of code in one of the classes from a project at work.

static private(set) var shared = UserPreferences()

I dont understand the use of (set) keyword in the above declaration. Tried normal googling but was unable to find anything useful. Can someone kindly explain its use? Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is not a set keyword but a private(set) keyword.

This makes the var accessible as internal but constricts the write (set) access to private.

So this value can only be set from inside the type it is defined in. But can be read from anywhere in the module that the type is defined in.

You can read more about this here… https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html#ID17

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading