6. Swift
— innovative new programming language for Cocoa and
Cocoa Touch
— introduced during WWDC 2014
— for iOS 7+
— for OS X 10.9+ (Mavericks, Yosemite, ...)
7. Nice Syntax
class Person {
func makeHello(who: String) -> String {
return "Hello, (who)!"
}
}
@interface Person : NSObject
- (NSString *)makeHello:(NSString *)who;
@end
@implementation Person
- (NSString *)makeHello:(NSString *)who {
return [NSString stringWithFormat:@"Hello, %@!", who];
}
@end
9. Type Safety
— encourages to be clear about the types
— type checks during compilation
— type inference
10. Type Inference
// these constants are of same type
let str1: String = "Foo"
let str2 = "Bar"
_stdlib_getDemangledTypeName(str1) // Swift.String
_stdlib_getDemangledTypeName(str2) // Swift.String
13. Optionals
var sureThing: String
var maybeNot: String?
maybeNot = "I might not be here"
sureThing = maybeNot // error
if let maybeConfirmed = maybeNot {
sureThing = maybeConfirmed
}
var dict = [String:String]()
dict["non_existent_key"] ?? "fallbackValue"
14. Tuples
— good for returning multiple results from a
function / method
func dices() -> (dice1: Int, dice2: Int) {
let d1 = Int(arc4random_uniform(6)) + 1
let d2 = Int(arc4random_uniform(6)) + 1
return (dice1: d1, dice2: d2)
}
let roll = dices()
roll.dice1 // something from 1...6
roll.dice2 // something from 1...6
15. Structs
struct Weather {
var temperature: Float
var sunny: Bool
func isForSunglasses () -> Bool {
return sunny
}
}
var perfectWeather = Weather(temperature: 30, sunny: true)
perfectWeather.temperature // 30
perfectWeather.sunny // true
Almost the same as a class, but it's a value type.
16. Value & Reference Types
struct Weather {
var temperature: Float
var sunny: Bool
}
var perfectWeather = Weather(temperature: 30, sunny: true)
var cloudyWeather = perfectWeather
cloudyWeather.sunny = false
perfectWeather.sunny // true - no change, because cloudyWeather have been copied and not referenced
17. Pattern Matching
— switch - no fallthrough
case 1..<10:
case (x, y) where x == y:
case .North:
case is UITableView:
18. Generics
— Array & Dictionary in Swift are generic
collections
func swapTwoValues<T>(inout a: T, inout b: T) {
let temporaryA = a
a = b
b = temporaryA
}
22. 1.1
— failable initializers
extension Int {
init?(fromString: String) {
if let i = fromString.toInt() {
self = i
} else {
return nil
}
}
}
— less SourceKitService crashes
23. 1.2
— migrator
— less SourceKitService crashes
— let constant must be initialized before use
if let a = foo(), b = bar() where a < b,
let c = baz() {
}
24. Swift Or Not To Swift?
!
— Education
— New Projects
— Experimenting
— Standalone libraries
25. Swift Or Not To Swift?
!
— Legacy Projects
— C++ In Project
— Support For iOS6 and older