Variables in SWIFT

Posted on 6/14/2014 @ 6:06 PM in #Swift by | Feedback | 1015 views

Every language needs variables. Swift has support for the following data types,

  1. Implicit types, Int, UInt, Float, Double
  2. Collection Types, Array and Dictionary
  3. Tuples, that are groupings of values, or can return multiple values from a single function
  4. Optional types, where you may not have a value, similar to nil pointer in ObjectiveC except optional types apply to both classes and value types, not just classes

When you declare a variable in Swift, you can declare it as a constant or a variable, as below

   1:  let iAmAConstant = 1
   2:  var iCanBechanged = "Changeable"

In the above example, the compiler has enough information to infer the data type you wish to provide. Sometimes you want to declare a variable, but provide the value later. This can be done as below,

   1:  var iCanBechanged:String, iCanAlsobeChanged:Int
   2:  iCanBechanged = "Ok I changed you"
   3:  iCanAlsobeChanged = 20

Concatenation of strings in SWIFT is really simple, like this,

   1:  var anotherString = "This is a complex string made up of \(iCanBechanged) and \(iCanAlsobeChanged)"

Sound off but keep it civil:

Older comments..