- Swift is a new language designed by Apple for developing iOS and OS X applications.
- It takes the best parts of C and Objective-C and adapts them with modern features and patterns. Swift-compiled programs will run on iOS7 or newer and OS X 10.9 (Mavericks) or newer.
- The two main goals for the language are compatibility with the Cocoa and Cocoa Touch frameworks and safety, as you’ll see in the upcoming chapters. If you’ve been using Objective-C, especially the modern syntax, Swift will feel familiar.
- However, Swift’s syntax is actually a major departure from Objective-C. It takes lots of cues from programming languages such as Haskell, C#, Ruby, and Python.
Type Inference
- you can figure out the type of the variable by the value it is assigned. There is usually no need to specify the type of variables , the types of the variables can be inferred by the value being set.
Type Safety
Control Flow
Optionals
- Swift variables must have a type and must be initialized before they can be used. Conversion between different types is done explicitly; you cannot simply assign a float to an integer.
- let doubleValue : Double = floatValue // This is an error
- The switch statement has undergone a major overhaul. Now it can select based not only integers, but also on strings, floats, and ranges of items, expressions, enums, and so forth. Moreover, there’s no implicit fall-through between case statements.
- Also introduced a guard statement. A guard statement is like an if statement but with an additional requirement of always having an else statement.
- A variable will either be nil or it will have a valid value. The nil value is distinct from any valid value. Optionals can also be chained together to protect against errors and exceptions.
- The String and Character types are also fully Unicode compliant and support various encodings, such as UTF-8, UTF-16, and 21-bit Unicode scalers.
- Header files are no longer required.
- Functions are full-fledged objects; they can be passed as arguments and returned from other functions. Functions can be scoped similarly to instance variables.
- Comments can be nested.
- There are separate operators for assignment (=) and comparison (==), and there’s even an identity operator (===) for checking whether two data elements refer to the same object.
- There is no defined entry point for programs such as main.