Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class variables not yet supported

Tags:

xcode

swift

I begin my project with a split view controller as initial view controller and start it automatically from storyboard.

Generally, an app with this UI have one and only one split view controller as root, so I create a static variable in the subclass and set it when initialisation was done.

So I want try this behaviour with swift.

I read the Swift programming language guide book on iBook about Type properties (with static and class keyword) and trying a piece of code to the job:

import UIKit  class SplitViewController: UISplitViewController {      class func sharedInstance() -> SplitViewController {         return SplitViewController.instance     }      class let instance: SplitViewController = nil      init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {         super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)         self.initialization()     }      init(coder aDecoder: NSCoder!) {         super.init(coder: aDecoder);         self.initialization()     }      func initialization() {         SplitViewController.instance = self;     } } 

but I figured out when Xcode say the class keyword for type properties wasn't supported yet.

error detail in image

Did you have a solution to do this ?

like image 509
Vincent Saluzzo Avatar asked Jun 03 '14 12:06

Vincent Saluzzo


People also ask

How do you declare a class variable in Swift?

Declaring VariablesYou begin a variable declaration with the var keyword followed by the name of the variable. Next, you add a colon, followed by the variable type. Afterward, you can assign a value to the variable using the (=) assignment operator.

How do you declare a class variable in Python?

A class variable is declared inside of class, but outside of any instance method or __init__() method. By convention, typically it is placed right below the class header and before the constructor method and other methods.

Can a class have public member variables?

Public membersPublic member variables and functions are accessible outside of the class. For example, you can change the values of variables through statements or functions outside of the class and you can call functions within the class from statements or other functions outside of the class.

Can classes have variables?

Class Variables Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object.


2 Answers

Embedding a struct can work just fine as a workaround:

class SomeClass {   // class var classVariable: Int = 0   // "Class variables not yet supported." Weird.    // Workaround:   private struct SubStruct { static var staticVariable: Int = 0 }    class var workaroundClassVariable: Int   {     get { return SubStruct.staticVariable }     set { SubStruct.staticVariable = newValue }   } } 

The SomeClass.workaroundClassVariable computed type property can then be used as if it were a stored type property.

like image 182
glessard Avatar answered Sep 27 '22 22:09

glessard


Swift now has support for static variables in classes. This is not exactly the same as a class variable (because they aren't inherited by subclasses), but it gets you pretty close:

class X {   static let y: Int = 4   static var x: Int = 4 }  println(X.x) println(X.y)  X.x = 5  println(X.x) 
like image 41
Bill Avatar answered Sep 27 '22 20:09

Bill