Private static variable is accessible in different classes? - ios

I declared a private static variable in one class. In the other file I could see them in the auto-complete. Is it some kind of a bug?
class Apple {
private static var name = "Apple"
}
In different file,
class Steve {
func call() {
// I could autocomplete, but it is not accessible as it says no member with the name
let name = Apple.name
}
}

After compile you can see auto-complete.

Related

Why private variable declared outside the class can be access Class on the same file?

While studying iOS stuffs, I encountered this private var declared outside the class on the same file that it can access the private var in any class. Private definition is any type that can be access only within the class itself, but not other class.
private let outside: String = "private-let?"
class LoginVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(outside)
}
}
Declaring any variables outside scope of class or struct will make it a Global Variable.
private let outside: String = "private-let?"
class LoginVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(outside)
}
}
So in your case even you create a private constant or variable globally it will be available inside all the class you defined in that file. So the access level will be considered to that file-level scope.
If you make it public it will be available in other file classes as well.
Read more about the variables here
Read more about the access control
Let say we have a file Helper.swift and since our private variable is outside the class which makes it Globally accessible.
You will not get any compile time error while accessing private member because your classes are in same file. But if two classes were in different file then you would get compile time error.
//Since this private variable is outside the class So its Global and accessible to any class within file.
private var secretCode : String? = nil
class MyPrivateData {
init() {
secretCode = "First change"
}
}
class MySecondClass {
init() {
secretCode = "Second change"
}
}
private is intended for use inside a type declaration. Otherwise it has no meaning. When private is used in a place where it has no meaning, it is reinterpreted as fileprivate. That is what happens here. This code is in the same file so the fileprivate variable is visible.

How to create Singleton in generic class using swift 4.2?

I have been trying to convert a singleton class to generic. Since swift doesn't support stored property in generic class Singleton could not be implemented. I tried all the possible solutions that I know but nothing works. Other Stackoverflow answers related to this topic doesn't help either. Can someone help me to get on the right path, please? Thanks in advance.
I have added the sample code below.
class SingleTonClass<T: Equatable & RawRepresentable>: NSObject where T.RawValue == String {
private var catlog: CatlogModelClass<T>?
private var catlogArray = [CatlogModelClass<T>]()
**// This stored property Now has to be changed to support Generic**
private static var shared : SingleTonClass = {
return SingleTonClass()
}()
// Accessor for Singleton
static func sharedInstance() -> SingleTonClass {
return shared
}
private override init () {}
}
If you want it to be non-singleton, you need to create a public constructor.
You should pass in the catalog and catalogArray into the constructor and initialize self.catalog and self.catalogArray. You then need to remove the static shared instance of the class, because your new setup would require you to create an instance of it.

Singleton in one line on Swift 2.0

Please help me with Swift,
I need singleton with can inheritance.
I can do like this
class A {
var defaultPort: Int
required init() {
self.defaultPort = 404
}
class var defaultClient: A {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: A? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = self.init()
}
return Static.instance!
}
}
but in swift 2.0 we can do like this
static let defaultClient = A() //self.init()
but it creates an instance of the class A any way.
How i can use like this self.init()
static let defaultClient = self.init()
in order to be able to inherit
UPD
best way for now
class A {
class func defaultClient() -> Self {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: A? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = self.init()
}
return instance(Static.instance, asType: self)
}
}
here we need helper as
func instance<T>(instance: Any, asType type: T.Type) -> T {
let reurnValue = instance as! T
return reurnValue
}
because another way cast A to Self not exist, for now.
p.s. crazy swift way!
why i can not do instance as! Self
Your question isn't very clear. You're looking for something like the class constant solution posted in this answer, but which automatically uses "my own class" instead of explicitly creating an instance of a specific class... right?
That is, you want to turn this:
class Singleton {
static let sharedInstance = Singleton()
}
into this:
class Singleton {
static let sharedInstance = SomeMagicThing()
}
class SingletonSubclass {}
where SomeMagicThing automatically creates a Singleton instance when you call Singleton.sharedInstance, and a SingletonSubclass instance when you call SingletonSubclass.sharedInstance. Correct?
Sorry, that can't be done (as of Swift 2.1).
Part of your issue is that static and class mean two different things. The static modifier means that the declaration it modifies is associated only with a specific type declaration. So, the Singleton type owns a pointer to a specific object -- its subclasses don't inherit that pointer. (And if they did, would it point to the same object or a subclass-specific one?)
If you could create a class var or class let, that'd (in theory) give you the kind of dispatch/inheritance you want. But trying that gives you an error (emphasis mine):
class stored properties not yet supported in classes; did you mean static?
So it sounds like this sort of thing might show up someday.
Of course, the other side of the problem is finding a way to dynamically refer to the "current" type responsible for executing some statement. In the context of an instance method, you have self.dynamicType for such things... but there's no equivalent for classes. (Self is a type constraint, not an actual type.) This is a side effect of the type system in Swift being much more strict and static than that of Objective-C (for example, metatypes aren't just a special flavor of otherwise normal objects). File a bug if you'd like to see a change to that effect?

Swift error with new xcode ' static properties are only allowed within structs and enums; use class. property'

I am getting the error on the following line.
static let sharedData : SharedData = SharedData()
I am using the new Xcode (6.2) so I don't know why I am getting this error. I thought this should've worked. Please let me know how to resolve this issue. Perhaps I should be using a different version.
Yes, static properties aren't yet allowed inside a class. (What is more, class is the keyword used to create static properties) Here is a work around for you to create static variables:
private struct SubStruct { static var sharedData:SharedData?}
class var sharedData:SharedData? {
get { return SubStruct.sharedData }
set { SubStruct.sharedData = newValue }
}
You can now refer the static variable as YourClass.sharedData.

How do you make a static class in swift?

I want to create a static class in swift, is this possible? If so how?
I tried:
static class MyClass
but get the error Declaration cannot be marked 'static'
There's no static class, but you can make one by just adding static methods only.
The problem is that (as of today) classes cannot have static properties, so you have 2 options:
use a struct instead of a class, defining all its methods and properties as static
use the singleton pattern
The second option is in my opinion a better solution, unless you have specific reason for not wanting it.
static means no instance, so I would make it a struct with no initializer:
struct MyStruct {
#available(*, unavailable) private init() {}
static var foo = "foo"
static func doSomething(a: String) -> String {
return a + foo
}
}

Resources