'Swift: is there any functional difference between "private static let" (in a class definition) and "fileprivate let" (no owner)
The question says it all really, but is there a functional difference between:
One way to define it:
class SomeClass {
private static let someValues: [String] = [
"mrah!",
"blah",
"shmah!"
]
var possibleValues: [String] {
return Self.someValues
}
}
or defined as:
class SomeClass {
var possibleValues: [String] {
return someValues
}
}
fileprivate let someValues: [String] = [
"mrah!",
"blah",
"shmah!"
]
Functionally identical, no? The defined array is inaccessible outside of the instance variable someValues Is there a reason not to do the latter?
(I want to include a bunch of long arrays in my class definition and you can't declare stored properties in extensions, and for the sake of code readability / brevity, I want to put these at the bottom of the implementation file, underneath other extensions.)
Solution 1:[1]
Your plan is fine, though you might as well mark the file-level value as private rather than fileprivate. They will behave the same. They are both even lazily initialized.
For full details on them, see "Global and Local Variables" and "Type Properties" in the Properties section of The Swift Programming Language.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rob Napier |
