Swift
Swift?
Dážďovník Obyčajný [SK]
— similar to swallow
— max. 169km/h
— cca. 200 000 km per year
Swift
— innovative new programming language for Cocoa and Cocoa Touch
— introduced during WWDC 2014
— for iOS 7+
— for OS X 10.9+ (Mavericks, Yosemite, ...)
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
Full Unicode Support
// Swiftlet smileAndCry = "!"let " = "like"
// Objective-C
Type Safety
— encourages to be clear about the types
— type checks during compilation
— type inference
Type Inference
// these constants are of same typelet str1: String = "Foo"let str2 = "Bar"
_stdlib_getDemangledTypeName(str1) // Swift.String_stdlib_getDemangledTypeName(str2) // Swift.String
Functional Programmingobjc.io/books
Immutability
let str = "Hello, World!"str = "Good bye!" // error
let arr = ["item1", "item2", "item3"]arr.append("item4") // error
struct Counter { var count = 0 mutating func increment() { count++ }}
Optionals
var sureThing: Stringvar 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"
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...6roll.dice2 // something from 1...6
Structs
struct Weather { var temperature: Float var sunny: Bool
func isForSunglasses () -> Bool { return sunny }}
var perfectWeather = Weather(temperature: 30, sunny: true)perfectWeather.temperature // 30perfectWeather.sunny // true
Almost the same as a class, but it's a value type.
Value & Reference Types
struct Weather { var temperature: Float var sunny: Bool}
var perfectWeather = Weather(temperature: 30, sunny: true)var cloudyWeather = perfectWeathercloudyWeather.sunny = falseperfectWeather.sunny // true - no change, because cloudyWeather have been copied and not referenced
Pattern Matching
— switch - no fallthrough
case 1..<10:case (x, y) where x == y:case .North:case is UITableView:
Generics
— Array & Dictionary in Swift are generic collections
func swapTwoValues<T>(inout a: T, inout b: T) { let temporaryA = a a = b b = temporaryA}
Generators, Sequences
protocol Generator { typealias Element func next() -> Element?}
for i in 1…10 { // do something}
Custom Operators
infix operator >>> { associativity left }
func >>> (filter1: Filter, filter2: Filter) -> Filter { return { img in filter2(filter1(img)) }}
let myFilter2 = blur(blurRadius) >>> colorOverlay(overlayColor)let result2 = myFilter2(image)
1
1 Excerpt From: “Functional Programming in Swift.“
REPLRead-Eval-Print-Loop (XCode, Terminal)
1.1
— failable initializers
extension Int { init?(fromString: String) { if let i = fromString.toInt() { self = i } else { return nil } }}
— less SourceKitService crashes
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() {}
Swift Or Not To Swift?!
— Education
— New Projects
— Experimenting
— Standalone libraries
Swift Or Not To Swift?!
— Legacy Projects
— C++ In Project
— Support For iOS6 and older
Thank You
Questions?