Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 387
Poles Avatar asked Jan 03 '16 09:01

Poles


People also ask

What is the use of static keyword in Swift?

In Swift, Static let and Static var are considered as Type Properties, which means that they can be accessed by their type. In this example, two properties have static keyword, one is constant and another one is variable.

How do you create a static class in Swift?

how would you create an all static class in Swift? static means no instance, so I would make it a struct with no initializer: struct VectorCalculator { @available(*, unavailable) private init() {} static func dotProduct(v: Vector, w: Vector) -> Vector { ... } }


2 Answers

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 
like image 198
Alex Pelletier Avatar answered Oct 10 '22 22:10

Alex Pelletier


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.

like image 42
Bobby Avatar answered Oct 10 '22 20:10

Bobby