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.
Did you have a solution to do this ?
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.
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With