private static variable in struct - swift

in this example getInstance is public so it makes sense to be accessible but the private variable was accessible as well, why?
public class SingleObject {
public struct Static {
private static var object: SingleObject?
public static func getObject() -> SingleObject {
if (object == nil) {
object = SingleObject()
}
return object!
}
}
}
SingleObject.Static.object //why private is accessible here?
SingleObject.Static.getObject()

As of Swift 3, the example code no longer compiles. A new modifier called fileprivate was added and works the way private used to work (i.e. it can be seen by anything else in the same file).
private in Swift 3 and 4 now works more like you would expect it to - in Swift 3, it can only be accessed within the context it was declared in, while in Swift 4 it can also be accessed from extensions to that type (but only if they're in the same file).

The private access modifier makes a declaration private to the file and not to the class or struct. That sounds a little weird, I know, but it's how it's implemented in swift.
The other 2 access modifiers are:
internal: accessible in the current module only
public: accessible from anywhere
Suggested reading: Access Control

The private keyword means that the variable is only accessible within the same file not within the same class.
You are accessing the property from within the same file, which is why it is working. Try doing it from a different file and you will see that it is not accessible.

Related

What's the difference of `private` and `fileprivate` class in the same file in Swift? [duplicate]

This question already has an answer here:
In Swift 3, is there a difference between 'private class Foo' and 'fileprivate class Foo' in regards to member variables?
(1 answer)
Closed 2 years ago.
As the sample code here, what's the difference of private and fileprivate class in the same file in Swift?
// ViewController.swift
private class A {
var name: String = ""
let d = B.D()
}
fileprivate class B {
var name: String = ""
private let c = C()
private class C {
var number: String = ""
}
fileprivate let d = D()
fileprivate class D {
var number: String = ""
}
}
Since class A and B are in the sample file, A can't be any more private then B, right?
Sorry this is a very picky question, but I just want to confirm it.
Access modifiers in swift have been charged a few times during the past few releases.
For now, let me suggest a code sample instead of explanation first:
All code in one file:
fileprivate class Foo {
private var someVariablePrivate: Int?
fileprivate var someVariableFileprivate: Int?
private func makeBar() { }
fileprivate func makeFoo() { }
}
extension Foo {
func bar() {
let value = someVariablePrivate // OK
let value2 = someVariableFileprivate // OK
}
}
private class Foofoo: Foo {
override func makeFoo() {
// OK
}
override func makeBar() {
// NOK, because private
}
}
class Bar {
func foo() {
let object = Foo()
let value = object.someVariableFileprivate // OK
let value2 = object.someVariablePrivate // NOK - because it private to that class
}
}
Previously, private restrict access only to type definition, but within few lates version of Swift, it has been modified and updated, so even extension of same type can use private variables
As Apple mention:
File-private access restricts the use of an entity to its own defining
source file. Use file-private access to hide the implementation
details of a specific piece of functionality when those details are
used within an entire file.
Private access restricts the use of an entity to the enclosing
declaration, and to extensions of that declaration that are in the
same file. Use private access to hide the implementation details of a
specific piece of functionality when those details are used only
within a single declaration.
we could compare a bit this modifiers:
why do we ned both of them:
You may, however, want to mark some parts of your code as file private
or private in order to hide their implementation details from other
code within the app’s module.
When to use fileprivate
Although the keywords are almost the same, there is a clear difference in their use cases. Fileprivate access restricts the use of an entity within the same defined source file. The only reason you would use fileprivate is when you want to access your code within the same file from different classes or structs.
When to use private
The private keyword is used a lot more and restricts the use of an entity to the enclosing declaration and its extensions. The extensions, however, have to be defined within the same file. In other words, private declarations will not be visible outside the file. You can use this keyword to only expose the minimal code needed to interact with the entity. This will improve readability and makes it easier to use and understand your code for others.
your question: Since class A and B are in the sample file, A can't be any more private then B, right?
In case of class A with private access specifier it won't be accessible outside it's declared scope.
In case of class B it will be accessible to the class A but it's private properties won't be accessible in class A
For more information, you can refer to this interesting article:
https://www.avanderlee.com/swift/fileprivate-private-differences-explained/#when-to-use-private

Private qualifier to an extension produces fileprivate behavior

Person.swift
class Person {
var name: String
init(name: String){
self.name = name
}
func greet() {
print("Hello, I am \(name)")
}
}
Workplace.swift
class WorkPlace {
var employees: [Person]
init(employees: [Person]) {
self.employees = employees
}
func greet() {
for employee in employees {
employee.customGreeting()
}
}
}
private extension Person {
func customGreeting() {
print("Hi")
}
}
Here I have a simple class called Person defined in Person.swift and a Workplace class defined in Workplace.swift.
The following is an excerpt taken from the Swift language guide (Access Control)
Alternatively, you can mark an extension with an explicit access-level modifier (for example, private) to set a new default access level for all members defined within the extension.
According to this I would expect the method customGreeting in the private extension of Place not be visible inside Person.swift as it would be a private method and it is defined in a different file than the one the class is declared in, which is exactly what happens. I would also expect that method to not be visible inside Workplace.swift but it does. I am able to compile this code without any errors.
If I mark the method as private explicitly then it is not visible inside Workplace.swift. Shouldn't specifying the extension as private be enough to make the method private like it says in the language guide?
private extension Person {
private func customGreeting() {
print("Hi")
}
}
I know that this is a contrived example and I am just trying to get a clear picture of how access control works in swift.
The issue isn't that methods declared in a private extension available in other classes, but rather, more narrowly, that private qualifier to an extension produces fileprivate behavior.
This is explicitly acknowledged in SE-0025, which says:
As before, an extension with an explicit access modifier overrides the default internal access by specifying a default scope. Therefore, within an extension marked private, the default access level is fileprivate (since extensions are always declared at file scope). This matches the behavior of types declared private at file scope.
This seems inconsistent with their broader statement:
... you can mark an extension with an explicit access-level modifier (for example, private) to set a new default access level for all members defined within the extension. This new default can still be overridden within the extension for individual type members.
While there is an inconsistency here, it would appear to be a conscious decision.

What is the difference between private and fileprivate in Swift 4

In Swift 4, since now private is visible in extensions also in the same source code file, how is it different from the fileprivate access modifier?
Background: In Swift 3, private variables in a class are not visible in its extensions in the same file. For that, fileprivate had to be used.
File Private
File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.
Syntax: fileprivate <var type> <variable name>
Example: fileprivate class SomeFilePrivateClass {}
Private
Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.
Syntax: private <var type> <variable name>
Example: private class SomePrivateClass {}
Here is more detail about all access levels: Swift - Access Levels
Answer to your question:
(In Swift 3, private variables in a class are not visible in its extensions in the same file. For that, fileprivate had to be used.)
Yes, in Swift 4.0, Private is now accessible in extension but within same file. If you declare/define extension in other file, then your private variable will not be accessible to your extension
Look at this images:
File: ViewController.swift
Here extension and view controller both are in same file, hence private variable testPrivateAccessLevel is accessible in extension
File: TestFile.swift
Here extension and view controller both are in different files, hence private variable testPrivateAccessLevel is not accessible in extension.
Here class ViewController2 is a subclass of ViewController and both are in same file. Here private variable testPrivateAccessLevel is not accessible in Subclass but fileprivate is accessible in subclass.
Applicable in swift 4.0 and its versions
Private
Private access only in class and its extension(When extension is in the same .swift file).
File Private
File-private access only in class and its extension & subClass(When extension or subClass is in the same .swift file).
///////////////ViewController1.swift file
class ViewController1 {
private func testPrivate() {
print("testPrivate")
}
fileprivate func testFilePrivate() {
print("testFilePrivate")
}
func doesNothing1() {
testPrivate() //success
testFilePrivate() //success
}
}
extension ViewController1 {
func doesNothingInExtensionSameFile() {
testPrivate() //success
testFilePrivate() //success
}
}
class SomeOtherClassInSameFile {
let vc1 = ViewController1()
func doesNothing() {
vc1.testPrivate() //throws error
vc1.testFilePrivate() //success
}
}
////////////// ViewController2.swift file
extension ViewController1 {
func doesNothingInExtensionDifferentFile() {
testPrivate() //throws error
testFilePrivate() //throws error
}
}
private and fileprivate access levels have come closer with Swift4.
The difference in access lies as follows:
fileprivate members - only & entirely within that .swift file
private members - only in that class & extension of the class if both are present in same .swift file
Hence only fileprivate members(not private) can be accessed in
Sub Classes in the same .swift file
Instances of the class (initialized in another class) in the same .swift file.
Open Vs Public:
Public does not allow a class to be inherited in another
module/target whereas Open does.
Public method does not allow to be overridden in subclass in another
module/target whereas Open does.
Apart from above both are same.
Private Vs Fileprivate:
(Within single file) Private does not allow to access (func and properties) in subclass whereas FilePrivate does.
(Outside File) Private and FilePrivate both can't be accessible.
Apart from above both are same.
"Private" is accessible only in class, "FilePrivate" accessible only in .swift file.
Private : Access in Class and Class Extension.
FilePrivate : Access in class, subClass, Extension,

Why not using static variable directly in Swift?

I found this in the Mozilla Firefox for iOS repository.
static let WebServerSharedInstance = WebServer()
class var sharedInstance: WebServer {
return WebServerSharedInstance
}
Why would you create a new variable that just returns the same variable when you could do the same in one line like this?
static let sharedInstance = WebServer()
I have looked into the code and I think I have misunderstood the context:
class WebServer {
static let WebServerSharedInstance = WebServer()
class var sharedInstance: WebServer {
return WebServerSharedInstance
}
}
While you can always use WebServer.WebServerSharedInstance to access the singleton, a subclass of WebServer can override sharedInstance.
class MyServer : WebServer {
override class var sharedInstance: MyServer {
return MyServer()
}
}
However, I have found no example in the code doing that so the original answer below is probably correct.
Original answer:
You are right. There is absolutely no reason to do that.
This has been probably translated directly from Objective-C, which uses similar syntax for singletons.
Also, with Swift 1.0 we weren't very sure how to create singletons and whether static let on a class is Thread safe.
I'm not sure the context. But let me explain one of the key differences between
// 1
class var sharedInstance: WebServer {
return WebServerSharedInstance
}
vs
// 2
static let sharedInstance = WebServer()
1 is computed variable which gets evaluated each time you access, while 2 get initialized lazily and evaluated only once.
If later in your code someone reassigns the WebServerSharedInstance
The 1 will return the new value while 2 will keep returning the initial value if it got initialized before.
A suggestion is that WebServerSharedInstance is a global variable declared at the top level of the file which lives as long as the file lives which is as long as the program runs --> You can use that variable in all other files

Understanding Singleton in Swift

I am trying out to create a singleton in SWIFT and this is what I have done so far
class Global {
class var sharedInstance:Global {
struct singleton {
static let instance:Global = Global()
}
return singleton.instance
}
}
var a = Global.sharedInstance
var b = Global()
if a === b {
println("Pointing to Same Instance")
}
else {
println("Pointing to different instance")
}
I have used computed type property to create a singleton (learnt that from another stackoverflow question).As of now the output is "Pointing to different instance".
What I am looking for is "a" and "b" in above example points to different instance of GLOBAL class and this breaks the point of singleton. How to make "a" and "b" in above example to point to the same instance of the class.
Thank you
This pattern does not guarantee there will only ever be one instance of the Global class. It just allows for anyone to access a single common instance of Global via its sharedinstance property.
So Global() declares a new instance of the Global class. But Global.sharedinstance does not create a new instance of Global, just fetches a pre-created one (that is created the first time anyone accesses it).
(If you alter your declaration of b to read var b = Global.sharedinstance you’ll see it confirms that a and b are pointing to the same instance.)
If you want to ban the creation of further instances of Global, make its init private:
private init() { }
But bear in mind you’ll still be able to create other Globals from within the file in which it’s declared, so if you’re doing the above in a playground or single-file test project, you won’t see any effect.
Class instance once in App life cycle.
class AccountManager {
static var sharedInstance = AccountManager()
var userInfo = (ID:"Arjun",Password:"123")
private init(){
print("allocate AccountManager")
}
}
here we set Private because :
Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.
also set static property of sharedInstance
because if you need to access class property without instance of class you must have to declare "Static".