How can I access a constant outside a class in swift? - class

class myClass {
let x = 0
}
How can I access the x constant outside myClass?

x is not defined as a constant, but as a mutable instance property. In order to make it immutable (which is technically different than constant, but the result doesn't change) you have to use let.
That said, if it's an instance property, you need a class instance, because the property is created as part of the class instantiation:
let instance = MyClass()
instance.x
If you want to make it a static property, accessible with the type and not with an instance of it, you should declare it as static:
class MyClass {
static let x = 0
}
However static stored properties are available in swift 1.2 only.
For previous versions you can either use a computed property:
class var x: Int { return 0 }
or turn the class into a struct:
struct MyClass {
static let x = 0
}
An alternative solution is to use a nested struct:
class MyClass {
struct Static{
static let x = 0
}
}
MyClass.Static.x

You could declare it as static var, like this:
class MyClass {
static var x = 0
}
and then you can access it outside the class with MyClass.x. If you declare it "class var", it will give you error message "Class stored properties not yet supported in classes; did you mean 'static'?", so they probably will be part of the language later. At this moment, you should do computed properties for class:
class MyClass {
static var x: Int {
return 3
}
} // This actually makes no sense to be a computed property though
You can find some info about Type Properties at: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html

Related

Swift: Why the non-static methods can't call static variables and constant (static let) without dynamicType?

It has destroyed my view about static variables and constant after using swift.
Why swift doesn't allow us to call static variables and constant in other methods?
for example:
class Aa {
static let name = "Aario"
func echo() {
print(name) // Error!
}
}
Mr. ogres told me to use dynamicType .
class Aa {
static var name = "Aario"
func echo() {
print(self.dynamicType.name)
}
}
let a = Aa()
a.dynamicType.name = "Aario Ai"
a.echo() // it works!!!
It works! So why should I use dynamicType to call static variables?
Finally, the answer is:
class Aa {
static var name = "Static Variable"
var name = "Member Variable"
func echo() {
print(self.dynamicType.name) // Static Variable
print(Aa.name) // Static Variable
print(name) // Member Variable
}
}
let a = Aa()
a.dynamicType.name = "Aario Ai"
Aa.name = "Static: Aario"
a.name = "Member: Aario"
a.echo() // it works!!!
It's really different with other languages.
Static variables have to be addressed with their type, even when you're writing code within this type. See The Swift Programming Language (Swift 2.2) - Properties (in "Querying and Setting Type Properties"):
Type properties are queried and set with dot syntax, just like instance properties. However, type properties are queried and set on the type, not on an instance of that type.
In your code, simply write Aa.name instead of name and you'll be fine.

Confusion Regarding Overriding Class Properties in Swift

I have read the Swift docs and searched here, but I still am not sure about how to implement a class hierarchy where each subclass sets custom value for an inherited static property; that is:
Base class defines a static property: all instances share the same value.
Subclass overrides the static property: all instances share the same value, which is different form that of the base class.
Can the property be stored?
Also, How should I access the value of the property from within an instance method (regardless of the particular class), and get the correct value everytime? will the following code work?
class BaseClass
{
// To be overridden by subclasses:
static var myStaticProperty = "Hello"
func useTheStaticProperty()
{
// Should yield the value of the overridden property
// when executed on instances of a subclass:
let propertyValue = self.dynamicType.myStaticProperty
// (do something with the value)
}
You are so close to being there, except that you can't override a static property in a subclass — that is what it means to be static. So you'd have to use a class property, and that means it will have to be a computed property — Swift lacks stored class properties.
So:
class ClassA {
class var thing : String {return "A"}
func doYourThing() {
print(type(of:self).thing)
}
}
class ClassB : ClassA {
override class var thing : String {return "B"}
}
And let's test it:
ClassA().doYourThing() // A
ClassB().doYourThing() // B

What is the use of "static" keyword if "let" keyword used to define constants/immutables in swift?

I'm little bit confused about using static keyword in swift. As we know swift introduces let keyword to declare immutable objects. Like declaring the id of a table view cell which most likely won't change during its lifetime. Now what is the use of static keyword in some declaration of struct like:
struct classConstants
{
static let test = "test"
static var totalCount = 0
}
whereas let keyword do the same.In Objective C we used static to declare some constant like
static NSString *cellIdentifier=#"cellId";
Besides which makes me more curious is the use of static keyword along with let and also var keyword. Can anybody explain me where to use this static keyword? More importantly do we really need static in swift?
I will break them down for you:
var : used to create a variable
let : used to create a constant
static : used to create type properties with either let or var. These are shared between all objects of a class.
Now you can combine to get the desired out come:
static let key = "API_KEY" : type property that is constant
static var cnt = 0 : type property that is a variable
let id = 0 : constant (can be assigned only once, but can be assigned at run time)
var price = 0 : variable
So to sum everything up var and let define mutability while static and lack of define scope. You might use static var to keep track of how many instances you have created, while you might want to use just varfor a price that is different from object to object. Hope this clears things up a bit.
Example Code:
class MyClass{
static let typeProperty = "API_KEY"
static var instancesOfMyClass = 0
var price = 9.99
let id = 5
}
let obj = MyClass()
obj.price // 9.99
obj.id // 5
MyClass.typeProperty // "API_KEY"
MyClass.instancesOfMyClass // 0
A static variable is shared through all instances of a class. Throw this example in playground:
class Vehicle {
var car = "Lexus"
static var suv = "Jeep"
}
// changing nonstatic variable
Vehicle().car // Lexus
Vehicle().car = "Mercedes"
Vehicle().car // Lexus
// changing static variable
Vehicle.suv // Jeep
Vehicle.suv = "Hummer"
Vehicle.suv // Hummer
When you change the variable for the static property, that property is now changed in all future instances.
Static Variables are belong to a type rather than to instance of class. You can access the static variable by using the full name of the type.
Code:
class IOS {
var iosStoredTypeProperty = "iOS Developer"
static var swiftStoredTypeProperty = "Swift Developer"
}
//Access the iosStoredTypeProperty by way of creating instance of IOS Class
let iOSObj = IOS()
print(iOSObj.iosStoredTypeProperty) // iOS Developer
//print(iOSObj.swiftStoredTypeProperty)
//Xcode shows the error
//"static member 'swiftStoredTypeProperty' cannot be used on instance of type IOS”
//You can access the static property by using full name of the type
print(IOS.swiftStoredTypeProperty) // Swift Developer
Hope this helps you..
to see the difference between type properties and / or methods and class properties and / or methods, please look at this self explanatory example from apple docs
class SomeClass {
static var storedTypeProperty = "Some value."
static var computedTypeProperty: Int {
return 27
}
class var overrideableComputedTypeProperty: Int {
return 107
}
}
Static properties may only be declared on type, not globally. In other words static property === type property in Swift. To declare type property you have to use static keyword.
"The let keyword defines a constant" is confusing for beginners who are coming from C# background (like me). In C# terms, you can think of "let" as "readonly" variable.
(answer to How exactly does the “let” keyword work in Swift?)
Use both static and let to define constant
public static let pi = 3.1416 // swift
public const double pi = 3.1416; // C#
public static final double pi = 3.1416 // Java
Whenever I use let to define constant, it feels like I am using readonly of C#. So, I use both static and let to define constant in swift.
Let me explain it for those who need Objective-C reference.
Hope you all remember that we were using a constant file in our Objective-C project to keep all the static API keys like below.
/ In your *.m file
static NSString * const kNSStringConst = #"const value";
Here the keyword Static does not mean that the kNSStringConst is a constant, it just defines that the kNSStringConst can be accessed globally. They keyword const makes it constant.
Now let's move to Swift.
In Swift, Static let and Static var are considered as Type Properties, which means that they can be accessed by their type.
For Example:
class World {
static let largestPopulation = "China"
static var secondLargestPopulation = "India"
}
World.largestPopulation = "German" // Cannot assign to property: 'largestPopulation' is a 'let' constant
World.secondLargestPopulation = "UK"
World.secondLargestPopulation // UK
In this example, two properties have static keyword, one is constant and another one is variable.
As you can see, the constant Static let declared can be accessed by it's type, but cannot be
changed.
The Static var declared can not only be accessed by it's type but also can be modified. Consequently, all the future instances will be changed.
[Swift's property]
[var vs let]
[class vs static]
In a nutshell class and static are type property and belongs to Type in a single copy

Example of dispatch_once in Swift

Is there an example of how dispatch_once should be used in Swift? (Preferably one from Apple.)
Note: In this case, I'm not using it for a singleton; I want to run arbitrary code exactly once.
Update: I'm mainly interested in the convention recommended when using this in an instance method, but usage in a class method, function, and in the global context would be useful for completeness sake.
dispatch_once_t is type alias (Int). Header documentation:
/*!
* #typedef dispatch_once_t
*
* #abstract
* A predicate for use with dispatch_once(). It must be initialized to zero.
* Note: static and global variables default to zero.
*/
typealias dispatch_once_t = Int
And here's the quote from dispatch_once documentation:
The predicate must point to a variable stored in global or static
scope. The result of using a predicate with automatic or dynamic
storage (including Objective-C instance variables) is undefined.
Token variable must be stored in global / static scope and must be initialized to zero, which leads to this code:
import Foundation
var token: dispatch_once_t = 0
dispatch_once(&token) { () -> Void in
print("Called once")
}
It doesn't work if you omit = 0 (token initialization), because compiler yields error Address of variable 'token' taken before it is initialized despite the fact that statics and globals default to zero. Tested in Xcode 7B2.
More examples based on comment. If you're inside class method you have several possibilities.
You can't declare static property inside method otherwise compiler yields Static properties may only be declared on a type error. This doesn't work:
class func doItOnce() {
static var token: dispatch_once_t = 0
...
}
Must be declared on a type. This was introduced in Swift 1.2 (Xcode 6.3 IIRC).
“static” methods and properties are now allowed in classes (as an
alias for “class final”). You are now allowed to declare static stored
properties in classes, which have global storage and are lazily
initialized on first access (like global variables). Protocols now
declare type requirements as “static” requirements instead of
declaring them as “class” requirements. (17198298)
So, what we can do if we don't like globals?
Static variable on a type
class MyClass {
private static var token: dispatch_once_t = 0
class func doItOnce() {
dispatch_once(&token) {
print("Do it once")
}
}
}
Static in a method wrapped in struct
Don't like static property on yur class? Would like to have it in your method? Wrap it in struct like this:
class func doItOnce() {
struct Tokens { static var token: dispatch_once_t = 0 }
dispatch_once(&Tokens.token) {
print("Do it once")
}
}
Actually I'm not aware of any Apple recommendation, best practice, ... how to do it for dispatch_once. Simply use whatever you like most, feels good to you and just meet criteria global / static scope.
For those of you who are curious, for me this approach has been useful for this purpose:
class SomeVC : UIViewController {
private var token: dispatch_once_t = 0
public override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
dispatch_once(&token) { () -> Void in
self.doSomethingOnce()
}
}
}
By not declaring a static var it has the expected behaviour. That being said, this is definitely NOT RECOMMENDED for any serious project, since in the Docs (as your well said) it states:
The predicate must point to a variable stored in global or static scope. The result of using a predicate with automatic or dynamic storage (including Objective-C instance variables) is undefined.
If we don't want to run into any future weird bugs and undefined behaviour I would just stick to what Apple says. But it's still nice to play around with these things, isn't it? =)
the robertvojta's answer is probably the best. because i try always to avoid import Foundation and use 'pure' Swift solution (with Swift3.0 i could change my opinion), I would like to share with you my own, very simple approach. i hope, this code is self-explanatory
class C {
private var i: Int?
func foo()->Void {
defer {
i = 0
}
guard i == nil else { return }
print("runs once")
}
}
let c = C()
c.foo() // prints "runs once"
c.foo()
c.foo()
let c1 = C()
c1.foo() // prints "runs once"
c1.foo()
class D {
static private var i: Int?
func foo()->Void {
defer {
D.i = 0
}
guard D.i == nil else { return }
print("runs once")
}
}
let d = D()
d.foo() // prints "runs once"
d.foo()
let d2 = D()
d.foo()

Swift: Class Type does not have member <var>

I have following code snippets:
protocol UpdateUIFromNativeListenerItf {
func triggerUI()
}
class WmBuildGroupsTask{
var mUfn:UpdateUIFromNativeListenerItf?
init(){/* ... */}
// ...
class func triggerRegister( ufn: UpdateUIFromNativeListenerItf ) {
mUfn = ufn // WmBuildGroupsTask.Type does not have `mUfn`
}
}
Form other class I call:
var ufn:UpdateUIFromNativeListenerItf = self
WmBuildGroupsTask.triggerRegister(ufn)
How can I pass delegate to static method?
Do I need create singleton for class WmBuildGroupsTask?
I tried also to write class var mUfn:UpdateUIFromNativeListenerItf?
But get: Class var not yet supported
Thanks,
Static stored properties are not supported in swift classes (yet), but they are in structs. So you can create an inline private struct and define your static properties there:
class WmBuildGroupsTask{
private struct Static {
static var mUfn:UpdateUIFromNativeListenerItf?
}
init(){/* ... */}
// ...
class func triggerRegister( ufn: UpdateUIFromNativeListenerItf ) {
Static.mUfn = ufn // WmBuildGroupsTask.Type does not have `mUfn`
}
}
The downside is that you have to access the static property prefixing it with the struct name - but I guess that's an acceptable tradeoff.
There is also another obvious way to solve the problem: turn the class into a struct, so obvious that it's enough to just mention it. Just a consideration: structs and classes are not interchangeable, they have their own pros and cons.
You are trying to access a member variable in a static context. mUfn is stored for an instance of your class, not for the class itself.