Slide 1

Slide 1 text

Kostiantyn Koval 1 Rocketfarm.no

Slide 2

Slide 2 text

Swift - Pushing technology limits

Slide 3

Slide 3 text

How should look Modern Language?

Slide 4

Slide 4 text

Clean and Clear

Slide 5

Slide 5 text

Programs must be written for people to read, and only incidentally for machines to execute 1 Harold Abelson

Slide 6

Slide 6 text

.global _start .text _start: # write(1, message, 13) mov $1, %rax mov $1, %rdi mov $message, %rsi mov $13, %rdx syscall message: .ascii "Hello, world\n" mov rax, 1 mov rdi, 1 mov rsi, message mov rdx, 13 syscall

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

int compare(const void * a, const void * b) { if ( *(uint32_t*)a < *(uint32_t*)b ) { return -1; } if ( *(uint32_t*)a > *(uint32_t*)b ) { return 1; } return 0; }

Slide 9

Slide 9 text

!

Slide 10

Slide 10 text

@interface Person : NSObject @property (nonatomic) NSString* name; @property (nonatomic) NSInteger age; @end @implementation Person - (instancetype)initWith:(NSString *)name age:(NSInteger)age { self = [super init]; if (self) return nil; _name = name; _age = age; return self; } @end @implementation TestPerson - (void)test { NSArray *people = @[ [[Person alloc] initWith:@"Sam" age:10], [[Person alloc] initWith:@"Sara" age:24], [[Person alloc] initWith:@"Ola" age:42], [[Person alloc] initWith:@"Jon" age:19]]; NSArray *kids = [people filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"age < 18"]]; NSMutableArray *names = [NSMutableArray new]; for (Person *person in people) { [names addObject:person.name.lowercaseString]; } } @end

Slide 11

Slide 11 text

!

Slide 12

Slide 12 text

struct Person { let name: String let age: Int } let people = [ Person(name: "Sam", age: 10), Person(name: "Sara", age: 24), Person(name: "Ola", age: 42), Person(name: "Jon", age: 19)] let kids = people.filter { person in person.age < 18 } let names = people.map { $0.name.lowercaseString }

Slide 13

Slide 13 text

!

Slide 14

Slide 14 text

60% Less Code 13 Lines Vs 34 Lines

Slide 15

Slide 15 text

Less Code Less Bugs Less money

Slide 16

Slide 16 text

Type inference var age = 19 var name = "Sara" var isEmpty = name.isEmpty VS var age: Int = 19 var name: String = "Sara" var isEmpty: Bool = name.isEmpty

Slide 17

Slide 17 text

No Semicolons ; var age = 19

Slide 18

Slide 18 text

Other 4 Closures 4 Default initializers 4 Default parameters 4 Subscripts 4 Operators

Slide 19

Slide 19 text

//Closures let numbers = [1, 2, 3, 4] numbers.map {$0 + 10} // Subscripts let num2 = numbers[2] //Operators infix operator <<< { } func <<< (a: Vector, b: Vector) -> Vector { return Vector(x: a.x + b.x, y: a.y + b.y) }

Slide 20

Slide 20 text

Smart

Slide 21

Slide 21 text

Features Rich

Slide 22

Slide 22 text

Features Reach 4 Functional Programming 4 OOP 4 Generic Purpose 4 Value types 4 Tuples 4 Optionals 4 ...

Slide 23

Slide 23 text

Safe

Slide 24

Slide 24 text

Safe 4 Types 4 Type casting 4 tUpos 4 Existing Methods 4 Correct instructions - Exception

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Error Handling func readFromFile(file: String) -> (result: String?, error: NSError?) { return ("file Content", nil) } let result = readFromFile("file.txt") if result.error != nil { println("handle error") }

Slide 27

Slide 27 text

Optionals

Slide 28

Slide 28 text

Optionals 4 Represent absence of a value 4 Safe Nil handling [NullPointerException] 4 Nil value for Value type, Int: 0, -1, NSnotFound, IntMax

Slide 29

Slide 29 text

'?' Type String != String? Int != Int? func person(name: String) -> Person func person(name: String, age: Int?) -> Person person("Elvis", age: nil) person(nil, age: 27) //Error

Slide 30

Slide 30 text

Optionals Is a Box that could store a value inside or could be empty You can interact with the value only throw Box API

Slide 31

Slide 31 text

Safe var person: Person? = people.find("Elvis") // 1 - Unwrapping `!` if person != nil { println("found \(person!)") } //2 - Optional binding if let person = person { println("found \(person)") } else { println("found found") }

Slide 32

Slide 32 text

Multithreading

Slide 33

Slide 33 text

Multithreading 4 Immutable value type 4 explicit 'self' capturing processAsync(var people: [Peope]) -> [Peope] { // ... Run Async people.remove(...) return people } var people: [People] //Async code processAsync(people) processAsync(people) processAsync(people)

Slide 34

Slide 34 text

Fast

Slide 35

Slide 35 text

Fast 18 Times > Objetive-C 2,9 Times > C

Slide 36

Slide 36 text

Let's make better mistakes tomorrow

Slide 37

Slide 37 text

Thanks! QA?? ! Twitter - @KostiaKoval GitHub - /KostiaKoval Kostiantyn Koval - Rocketfarm.no