Page 24

Swift

Type Safety Swift is a type-safe language which means if a part of your code expects a String, you can't pass it an Int by mistake. As Swift is type-safe, it performs type-checks when compiling your code and flags any mismatched types as errors. import Cocoa

var varA = 42 varA = "This is hello" println(varA) When we compile the above program, it produces the following compile time error. Playground execution failed: error: :6:6: error: cannot assign to 'let' value 'varA' varA = "This is hello"

Type Inference Type inference enables a compiler to deduce the type of a particular expression automatically when it compiles your code, simply by examining the values you provide. Swift uses type inference to work out the appropriate type as follows. import Cocoa

// varA is inferred to be of type Int var varA = 42 println(varA)

// varB is inferred to be of type Double var varB = 3.14159 println(varB)

// varC is also inferred to be of type Double var varC = 3 + 0.14159 println(varC)

12

​Quick is a strong and intuitive development language for macOS, iOS, watchOS and tvOS....  

...

Advertisement