Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(set) keyword in swift

Tags:

swift

I was recently browsing through the DemoBots example from Apple and I came across:

/// The scene that is currently being presented.
private (set) var currentSceneMetadata: SceneMetadata?

What exactly does (set) do and what other options (if any) are available here?

like image 899
Kyle Decot Avatar asked Jan 08 '23 01:01

Kyle Decot


1 Answers

It means only setter is private. So currentSceneMetadata access for get is the default--which is internal-- but that of set is private. So it can only be changed from within the same source file.

Link to confirm: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html

EDIT: You could even do something like this:

public private (set) var name: String

which means name's access level for setter is private and for getter is public.

like image 117
Peyman Avatar answered Jan 18 '23 21:01

Peyman