Top Banner
Swift
27

CocoaHeads Bratislava #1 - Swift

Aug 08, 2015

Download

Software

EskiMag
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CocoaHeads Bratislava #1 - Swift

Swift

Page 2: CocoaHeads Bratislava #1 - Swift

Swift?

Page 3: CocoaHeads Bratislava #1 - Swift
Page 4: CocoaHeads Bratislava #1 - Swift

Dážďovník Obyčajný [SK]

— similar to swallow

— max. 169km/h

— cca. 200 000 km per year

Page 5: CocoaHeads Bratislava #1 - Swift
Page 6: CocoaHeads Bratislava #1 - Swift

Swift

— innovative new programming language for Cocoa and Cocoa Touch

— introduced during WWDC 2014

— for iOS 7+

— for OS X 10.9+ (Mavericks, Yosemite, ...)

Page 7: CocoaHeads Bratislava #1 - Swift

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

Page 8: CocoaHeads Bratislava #1 - Swift

Full Unicode Support

// Swiftlet smileAndCry = "!"let " = "like"

// Objective-C

Page 9: CocoaHeads Bratislava #1 - Swift

Type Safety

— encourages to be clear about the types

— type checks during compilation

— type inference

Page 10: CocoaHeads Bratislava #1 - Swift

Type Inference

// these constants are of same typelet str1: String = "Foo"let str2 = "Bar"

_stdlib_getDemangledTypeName(str1) // Swift.String_stdlib_getDemangledTypeName(str2) // Swift.String

Page 11: CocoaHeads Bratislava #1 - Swift

Functional Programmingobjc.io/books

Page 12: CocoaHeads Bratislava #1 - Swift

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++ }}

Page 13: CocoaHeads Bratislava #1 - Swift

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"

Page 14: CocoaHeads Bratislava #1 - Swift

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

Page 15: CocoaHeads Bratislava #1 - Swift

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.

Page 16: CocoaHeads Bratislava #1 - Swift

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

Page 17: CocoaHeads Bratislava #1 - Swift

Pattern Matching

— switch - no fallthrough

case 1..<10:case (x, y) where x == y:case .North:case is UITableView:

Page 18: CocoaHeads Bratislava #1 - Swift

Generics

— Array & Dictionary in Swift are generic collections

func swapTwoValues<T>(inout a: T, inout b: T) { let temporaryA = a a = b b = temporaryA}

Page 19: CocoaHeads Bratislava #1 - Swift

Generators, Sequences

protocol Generator { typealias Element func next() -> Element?}

for i in 1…10 { // do something}

Page 20: CocoaHeads Bratislava #1 - Swift

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.“

Page 21: CocoaHeads Bratislava #1 - Swift

REPLRead-Eval-Print-Loop (XCode, Terminal)

Page 22: CocoaHeads Bratislava #1 - Swift

1.1

— failable initializers

extension Int { init?(fromString: String) { if let i = fromString.toInt() { self = i } else { return nil } }}

— less SourceKitService crashes

Page 23: CocoaHeads Bratislava #1 - Swift

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() {}

Page 24: CocoaHeads Bratislava #1 - Swift

Swift Or Not To Swift?!

— Education

— New Projects

— Experimenting

— Standalone libraries

Page 25: CocoaHeads Bratislava #1 - Swift

Swift Or Not To Swift?!

— Legacy Projects

— C++ In Project

— Support For iOS6 and older

Page 26: CocoaHeads Bratislava #1 - Swift

Thank You

Page 27: CocoaHeads Bratislava #1 - Swift

Questions?