Swift 2

Page content
Value types in swift are:

structs (incl. arrays and dictionaries) enumerations basic data types (boolean, integer, float, etc.)

Implicit

Swift doesn’t define any implicit cast between data types, even if they are conceptually almost identical (like UInt and Int).

To fix the error, rather than casting, an explicit conversion is required. In the sample code, all expression operands must be converted to a common same type, which in this case is Double:

var result = Double(op1) + Double(op2) + op3

In Swift enumerations, what’s the difference between raw values and associated values?

To solve the problem it’s necessary to break at least one of the 2 strong relationships, by using either the weak or unowned modifier. The differences between the 2 modifiers is:

unowned: the reference is assumed to always have a value during its lifetime - as a consequence, the property must be of non-optional type. weak: at some point it’s possible for the reference to have no value - as a consequence, the property must be of optional type.

However, you can optionally allow such modification to occur by declaring the instance methods as ‘mutating’; e.g.:

struct IntStack {
  var items = [Int]()
  mutating func add(x: Int) {
    items.append(x) // All good!
  }
}

functions:

def someFnWithFirstArg(arg1 firstArg:String, _ secondArg:String)->{
firstArg+secondArg
}

someFnWithFirstArg(arg1:"fghfh","fgfh")

Note: In swift its not garbage collection, its reference counting. Object always lives in the heap, so no & or *.

  • In swift all properties have to initialized when the object is initialized