Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I initialize a static variable by calling a static function in Swift?

Tags:

ios

swift

It is my understanding that as of Xcode 6.3 / Swift 1.2, I can use static variables and methods inside of a class. However, the compiler doesn't like it when I try to initialize a static variable by calling a static function (I get the error 'Use of unresolved identifier getDefaultString' in the example below). Here is a snippet that demonstrates my problem:

import Foundation

public class Settings {
    private static var _bundle = NSBundle.mainBundle()
    static func getDefaultString(key: String) -> String {
        return _bundle.objectForInfoDictionaryKey(key) as! String
    }

    private static var _server = getDefaultString("DefaultServer")
    public class var server: String {
        get { return _server }
        set { _server = newValue }
    }
}

Can someone help me understand why I can't do this?

like image 718
markdb314 Avatar asked Jun 15 '15 20:06

markdb314


People also ask

Can we initialize static variable in static method?

Static variables are initialized only once , at the start of the execution. These variables will be initialized first, before the initialization of any instance variables. A single copy to be shared by all instances of the class. A static variable can be accessed directly by the class name and doesn't need any object.

How do I create a static variable in Swift?

You create static variable by appending static keyword in front of your variable declaration. We will be using playground to explore more. When we define any variable as let, it means it's values cannot be modified, On the other hand if we define any variable as var it means it's values can be modified.

Can static variables be initialized with any expression?

A static variable can be initialized only with a constant expression.

How do you declare a variable in a static method?

Static variables in methods i.e. you cannot use a local variable outside the current method which contradicts with the definition of class/static variable. Therefore, declaring a static variable inside a method makes no sense, if you still try to do so, a compile time error will be generated.

What is initialization of static variables?

Initialization of static variables Initialization of static variables You initialize a staticobject with a constant expression, or an expression that reduces to the address of a previously declared externor staticobject, possibly modified by a constant expression.

Why do we need initializers in Swift?

Unlike Objective-C initializers, Swift initializers don’t return a value. Their primary role is to ensure that new instances of a type are correctly initialized before they’re used for the first time.

What are static variables in C++?

Static variables are those variables whose values are shared among all the instance or object of a class. When we define any variable as static, it gets attached to a class rather than an object. The memory for the static variable will be allocation during the class loading time.

Do Swift subclasses inherit their superclass initializers?

Unlike subclasses in Objective-C, Swift subclasses don’t inherit their superclass initializers by default. Swift’s approach prevents a situation in which a simple initializer from a superclass is inherited by a more specialized subclass and is used to create a new instance of the subclass that isn’t fully or correctly initialized.


1 Answers

This should work:

private static var _server = Settings.getDefaultString("DefaultServer")

I do not know exactly why, it seems that there is some sort of an assumption in the play that a method without type qualified in front of it is an instance method. But that does not fully work as well. E.g. this produces interesting result in playground:

    class Foo {

        static func initFoo() -> String {
            return "static"
        }

        func initFoo() -> String {
            return "instance"
        }

        static var foo: String = initFoo()

    }

    Foo.foo

... no need to qualify type in front of initFoo(), but still the static one is picked up. A minor bug probably.

like image 78
0x416e746f6e Avatar answered Oct 01 '22 06:10

0x416e746f6e