Swift 2 changes

Structs are value types

classes are reference types

  • print()
Array:

eg:
var arr:Array<Int> = [] or
var arr:[Int] = []

for mixed values you have to explicitly mention:
var mixed:[Any] = [1, "str", false]
var mixed:[AnyObject] = [1, "str"]

mixed.append(2, atIndex:3)

mixed.insert(2, atIndex:3)

mixed.remoteAtIndex(2, atIndex:3)

.. all the way upto
... all the way upto and including
..<

Array Slicing:

let slicedArray = arr[1..<3] // when the item is sliced like above the indexes of the slicedArray remains the same so slicedArray[0] will return an error

Array enumerate() to get the index


for (index, item) in arr.enumerate() {
  print("index of \(item) is \(index)")
}

Dictionary

2 ways of declaring:
eg:

var dct: Dictionary = ["":""]
var dct: [String : String] = ["":""]; dct = ["raj":1, "krish":2]
dct["raj"] = nil`;

for (key, value) in dct {
   print("\(key) in \(value)")
}

Optionals:

var x: Int?
x = 12
var y:Int?

if x != nil {
print("(x!)")
}

//Optional binding (shortcut for above)
if let z = x {
print("(z)")
}

if let xx = x {
if let yy = y {
print(xx*yy)
}
}

// in swift 2 - multiple conditions in one line
if let xx = x, let yy = y {
print(xx*yy)
}

if let xx = x where xx % 2 == 0 {
print(xx)
}