Денис Лебедев, swift

49
Swift Denis Lebedev, iOS @ Yandex

Upload: yandex

Post on 10-May-2015

414 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Денис Лебедев, Swift

Swift

Denis Lebedev, iOS @ Yandex

Page 2: Денис Лебедев, Swift

Agenda

• Introduction

• Objective-C bridging

• Good-to-know features

• “Where can I Swift?”

Page 3: Денис Лебедев, Swift

Swift

Chris Lattner, Swift creator

Page 4: Денис Лебедев, Swift

Swift

• Multi-paradigm

• Static, inferred typing

• Bridged with Objective-C

• No pointers

Page 5: Денис Лебедев, Swift

–Someone at WWDC Keynote

It’s like “Objective-C without C.”

Page 6: Денис Лебедев, Swift
Page 7: Денис Лебедев, Swift

Swift features

• Namespacing*

• Generic classes & functions

• Named/default parameters

• Functions are first class citizens

• Optional types

Page 8: Денис Лебедев, Swift

Optionals

• Used in situations where value may be absent

• Alternative for obj-c nil passing

• works with any type

Page 9: Денис Лебедев, Swift

Optionals

- (NSInteger)indexOfObject:(id)object;

Page 10: Денис Лебедев, Swift

Optionals

func indexOf(object: AnyObject) -> Int?

Page 11: Денис Лебедев, Swift

Forced unwrapping

let result :Int? = indexOf(bar);

let s = String(result) //error

let s = String(result!)

Page 12: Денис Лебедев, Swift

Optional binding

if let r = result {

let s = String(r)

} else {

// r is nil

}

Page 13: Денис Лебедев, Swift

Classes, structs, enums

• Classes passed by reference, structs - by value

• Using a struct has no runtime penalty

• All scalars and even Bool are structs

Page 14: Денис Лебедев, Swift

Collections

• Array, Dictionary

• Collections are structs

• Implicitly bridged to Cocoa collection types

Page 15: Денис Лебедев, Swift

Built-in immutability

var b = 3

b += 1

let a = 3

a += 1 // error

Page 16: Денис Лебедев, Swift

Dictionary immutability

let d = ["key0": 0]

d["key"] = 3 //error

d.updateValue(1, forKey: "key1") //error

Page 17: Денис Лебедев, Swift

Array immutability

let c = [1, 2, 3]

c[0] = 3 // success

c.append(5) // fail

Page 18: Денис Лебедев, Swift

Array immutability

let c = [1, 2, 3]

c[0] = 3 // success

c.append(5) // fail

It’s a bug:

https://devforums.apple.com/message/971330#971330

Page 19: Денис Лебедев, Swift

Extensions

• extends any named type (struct, enum, class)

• structures code

Page 20: Денис Лебедев, Swift

Extensions struct Foo {

let value : Int

}

extension Foo : Printable {

var description : String {

get {return "Foo"}

}

}

extension Foo : Equatable {

}

func ==(lhs: Foo, rhs: Foo) -> Bool {

return lhs.value == rhs.value

}

Page 21: Денис Лебедев, Swift

What Swift is missing

• Preprocessor

• Exceptions

• Access control *

• KVO, KVC

• Compiler attributes (platforms, deprecations, etc.)

• performSelector: is unavailable

Page 22: Денис Лебедев, Swift

Objective-C bridging

• Call Obj-c from Swift

• Call Swift from Objc with limitations

• Call CoreFoundation types directly

• C++ is not allowed (should be wrapped in Objc)

• Subclassing Swift classes not allowed in Objc

Page 23: Денис Лебедев, Swift

Objective-C bridging

• NSArray < - > Array

• NSDictionary < - > Dictionary

• NSNumber - > Int, Double, Float

Page 24: Денис Лебедев, Swift

Objective-C bridging

@objc class Foo {

init (bar: String) { /*...*/ }

}

Page 25: Денис Лебедев, Swift

Objective-C bridging

@objc(objc_Foo)

class Foo {

@objc(initWithBar:)

init (bar: String) { /*...*/ }

}

Page 26: Денис Лебедев, Swift

Objective-C bridging

Foo *foo = [[Foo alloc] initWithBar:@"Bar"];

Page 27: Денис Лебедев, Swift

Objective-C bridging

func tableView(tableView: UITableView!,

cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

Page 28: Денис Лебедев, Swift

Objective-C bridging

• All object types are mapped as implicitly

unwrapped optionals (T!)

• All ‘id’ types are mapped as ‘AnyObject’

Page 29: Денис Лебедев, Swift

Swift internals

• Swift objects are Obj-c objects

• Implicit root class ‘SwiftObject’

• Ivars type encoding is stored separately

• Method’s vtable

Page 30: Денис Лебедев, Swift

Name mangling

• Swift keeps function metadata encoded in

function symbols

Page 31: Денис Лебедев, Swift

Name mangling

class Foo {

func bar() -> Bool {

return false

}

}

_TFC9test3Foo3barfS0_FT_Sb

Page 32: Денис Лебедев, Swift

Compiler features

• Profile-guided optimization (‘off’ by default)

• Loop vectorisation (‘on’ by default from Xcode 6)

• High-level language-specific optimizer and IR

before LLVM (‘xcrun swift t.swift -O -emit-sil')

Page 33: Денис Лебедев, Swift

Performance

• 10-100 x slower than C++ (-O0)

• 10 x slower than C++ (-O3)

• 1 x as C++ (-Ofast)*

Page 34: Денис Лебедев, Swift

Performance

• Swift is still in beta

• Unoptimized calls of retain/release in loops

Page 35: Денис Лебедев, Swift

Pattern matching

let point = (0, 1)

if point.0 == 0 && point.1 == 0 {

println("Point is at the origin")

} else if point.0 >= 0 && point.0 <= 1 &&

point.1 >= 0 && point.1 <= 1 {

println("I")

}

...

Page 36: Денис Лебедев, Swift

Pattern matching let point = (0, 1)

switch point {

case (0, 0):

println("Point is at the origin")

case (0...1, 0...1):

println("I")

case (-1...0, 0...1):

println("II")

case (-1...0, -1...0):

println("III")

case(0...1, -1...0):

println("IV")

default:

println(“I don’t know.")

}

Page 37: Денис Лебедев, Swift

Function currying

func add(a: Int)(b: Int) -> Int {

return a + b

}

let foo = add(5)(b: 3) // 8

let add5 = add(5) // (Int) -> Int

let bar = add5(b: 3) // 8

Page 38: Денис Лебедев, Swift

Auto closures

• Wraps function argument in explicit closure

func assert(condition:() -> Bool, message: String) {

#if DEBUG

if !condition() { println(message) }

#endif

}

assert({5 % 2 == 0}, "5 isn't an even number.")

Page 39: Денис Лебедев, Swift

Cross-platform code

#if os(iOS)

typealias View = UView

#else

typealias View = NSView

#endif

class MyControl : View {

}

Page 40: Денис Лебедев, Swift

Auto closures Wraps function argument in explicit closure

func assert(condition: @auto_closure () -> Bool,

message: String) {

#if DEBUG

if !condition() { println(message) }

#endif

}

assert(5 % 2 == 0, "5 isn't an even number.")

Page 41: Денис Лебедев, Swift

Implicit type conversion

struct Box<T> {

let _value : T

init (_ value: T) {

_value = value

}

}

let value = Box(1)

value + 3 //error

Page 42: Денис Лебедев, Swift

Implicit type conversion

extension Box {

@conversion func __conversion() -> T {

return value

}

}

let value = Box(3 + 3)

value + 3 //9

Page 43: Денис Лебедев, Swift

Implicit type conversion

• allows any type to be ‘nil’ (which has NilType)

• allows toll-free-bridging with Cocoa types

Page 44: Денис Лебедев, Swift

Reflection

struct Foo {

var str = "Apple"

let int = 13

func foo() { }

}

reflect(Foo()).count // 2

reflect(Foo())[0].0 // "str"

reflect(Foo())[0].1.summary // "Apple

Page 45: Денис Лебедев, Swift

Direct call of C functions

@asmname - attribute that allows to provide a Swift interface for C functions

@asmname("my_c_func")

func my_c_func(UInt64, CMutablePointer<UInt64>) -> CInt;

Page 46: Денис Лебедев, Swift

Scripting and REPL

• xcrun swift - launches REPL

• xcrun -i ‘file.swift’ - executes script

Page 47: Денис Лебедев, Swift

Where can I swift?

• BDD Testing framework: Quick

• Reactive programming: RXSwift

• Model mapping: Crust

• Handy JSON processing: SwiftyJSON

Page 48: Денис Лебедев, Swift

Thx!

@delebedev

Page 49: Денис Лебедев, Swift

Credits

• http://nondot.org/sabre/

• https://devforums.apple.com/thread/227288

• http://andelf.github.io/blog/2014/06/08/swift-implicit-type-cast/

• https://www.youtube.com/watch?v=Ii-02vhsdVk

• http://www.eswick.com/2014/06/inside-swift/

• http://article.gmane.org/gmane.comp.compilers.clang.devel/37217

• http://stackoverflow.com/questions/24101718/swift-performance-sorting-arrays

• http://www.splasmata.com/?p=2798

• https://github.com/rodionovd/SWRoute/wiki/Function-hooking-in-Swiftкак