Quick viewing(Text Mode)

Theory Static More Static Than Objective-C Compiled Not

Theory Static More Static Than Objective-C Compiled Not

static More static than Objective-

compiled not interpreted

Type Inference

Object-Oriented

Functional

Theory what type of generic? There was a Generic technical term. Opposite of „type erasure“

„13 of the security issues fixed in 10.9.4 could have been prevented or mitigated by Swift (9 bufer overruns, 2 int under/overflows, 2 null Much safer than Objective-C and C derefs). That alone makes it worth it from my point of view.“ https://devforums.apple.com/message/ 1007093#1007093

value type can be different for each member of the enumeration (B1: p. Enums with associated values 143) other names: „discriminated unions“, „tagged unions“, „variants“

normal switch

combine several cases together but in that case no value bindings

no automatic fall-through, only explicit

must be exhaustive (use default if in doubt)

switch over strings

switch over Any type via probably via ==

let und var switch over ranges

Type syntax switch by type Pattern matching Return type with arrow use wildcard-operator `_` match tuple automatic external parameter partil type restrictions in tuples (x: String, y) names for every parameter initializer value bindings using let/var can use _ to prevent an external parameter name. But shouldn’t. match associated value of enum

parameter names are not used for Local and external parameter complex patterns combined from calling by default. Can specify names all of the above external parameter names explicitly functions or use same as internal with `#` guard expression: can specify prefix arbitrary additional checks with `where` all parameter names are requied, methods overload ~= operator to extend except first that is assumed Parameters pattern matching Can declare a parameters as var to Function syntax allow code to change it inside the strong by default variable use whenever it is valid to become can annotate with special „inout“ weak-references nil during lifetime keyword that makes changes to the variable propagate to the calling always Optional<> site. use when reference will never be nil example: NSLocalizedString with once it has been set during default values for parameters default values for bundle and table initialization

given as array to function variadic parameters ARC in Swift similar to unsafe unretained

Nested functions accessing an unowned reference unowned references after the referenced instance is override keyword deallocated is a runtime error

static/class methods App will always crash when unowned deallocated instance is Classes referenced. => Safer than unsafe unretained Structs capture lists in closures Enums Advanced variables can be changed by can adopt protocols default (like __block in Objective-C). can have methods But simply use let instead. All can have properties Closures user @lazy properties when referring to self (can’t refer to self in Use closures to initialize properties can use generics a property-initializing non-lazy Difference between Classes, with more complex calculations Structs and Enums Types closure, as self is not yet initialized special „mutating“ functions Constant behaviour at that point in time) Reference-Types and Value Types Copying behaviour 1. initialize own properties

Inheritance Basics 2. call super.init designated initializers must call super-types init correct initialization order Static properties 3. modify properties of superclass and call instance methods Basic Initialization 1. call self.init this can only be adopted by classes can only have optional methods if convenience initializers must call across to self.init correct initialization order marked with @objc Protocols 2. modify properties of superclass and call instance methods can even specify initializers initializers if all properties initialized: zero- Stored Properties default initializers argument-init Lazy Properties Properties structs: all-arguments init Computed Properties if all properties initialized: zero- stringByAppendingString: crashes argument-init when given „nil“ Initializer inheritance use three examples of functions to nil is a special value for every type, if all designated initializers override: indexOfObject: returns show why making optionality „normal“ values for a type can’t be inherit convenience initializers NSNotFound when the collection explicit is a good thing nil required-attribute forces subclass to override it does not contain the object computed variables if-let Optionals Variables lazy variables ? operator global variables are always lazy ! operator @auto_closure if-let Special attributes Swift @lazy result of optional chaining is always an optional, but at most one level of optional-chaining testing if methods exists optionality is added (B1, p.255) functions without a return-type check whether it is of that type is have an implicit return type of Void/ optional chaining (). That means their optional- cast if of that type (therefore return as? chaining return-type is Void? and type optional) can thus be used to see whether forced cast as the function was called. (B1, p.253) Casting can cast complete collections, e.g. can extend every type of Swift, [AnyObject] as [Car] even structs, enums and e.g. Int (which is just a struct) but only if marked with @objc also work for checking for protocol conformance Extensions add functionality to the original type, that wasn’t there before (like Can define other types inside of Nested Types Categories) type definition (B1, p.266) add protocol conformance static properties Operator Overloading like -class in Objective-C, returns Operators dynamicType Types the Type of an object/value Own Operators

As in `Array.self` .self Still slow

tyepalias name = existingType typealias Syntax still changes .. became ..<

Higher Order Functions Constant arrays are now completely constant but weren’t at first Closures Semantics still change Beta Character is said to support Infinite Sequence of Fibonacci Functional Programming Lazy-Evaluation of Sequences complete graphemes soon numbers access control: private/public Currying Missing features Reflection? overriding of methods, as in Objective-C KVO? Subtyping/Dynamic Polymorphism but @selector is available, e.g. for single dispatch no performSelector: setTarget:action: dynamic dispatch/late binding Polymorphism What do we loose? KVC and KVO? overloading functions as in Java Ad Hoc Polymorphism/Function partially enabled if subclass of compile time/early binding Message Passing overloading based on parameters NSObject not possible in Java Also polymorphism on return type Primitive types are also structs

Generic programming is a programming vtable instead of dynamic dispatch method that is based in finding the most Swift internals abstract representations of efficient more compiler optimizations algorithms. http://www.stlport.org/ possible because of more resources/StepanovUSA.html constants and less unclear pointers

„find the [generic] data structure inside var temp:UInt32 = 0 an algorithm“ instead of „find the for (var i = 0; i <= 100_000_000; i++) [virtual] algorithms inside an object“ { http://www.stlport.org/resources/ temp ^= 12345678 StepanovUSA.html } result = temp template void Swap(T & a, T & b) //„&“ Simple Loop (100_000_000) C: 0.030 passes parameters by reference -Os/ -O { T temp = b; Swift: 0.059 b = a; a = temp; C: 0.011 } swap -Ofast string hello = „world!“, world = Swift: 0.011 „Hello, „; Generic Programming Swap( world, hello ); Examples int temp = 0; cout << hello << world << endl; // for (int i = 0; i < count; i++) Output is „Hello, world!“ { temp += numbers[i]; } max result = temp;

Generic Collections C: 0.055 difference to generic parameter: Associated Types (only defined Generic Protocols Swift-for-loop: 1.712 has to be specified by subtypes later) Swift-for-in-loop: 5.136 http://en.wikipedia.org/wiki/ -Os/ -O Generic_programming Swift-reduce: 4.961

Subclasses of generic classes must Objective-C-for-loop: 2.418 also be generic Sum of Array Limitations in Swift Objective-C-fast-enumeration: e.g. Bag extends Cannot modify generic parameter in 1.210 Array subclass C: 0.028 Swift and Objective-C Performance Swift-for-loop: 0.058

Swift-for-in-loop: 0.156

Measurements -Ofast Swift-reduce: 0.072

Objective-C-for-loop: 2.459

Objective-C-fast-enumeration: 1.211

Array(count: HUNDRED_MILLION, repeatedValue:42)

Swift: 0.471 -Os/ -O Create array with fixed values Objective-C-for-loop: 4.320

Swift: -Ofast Objective-C-for-loop: 2.459

Swift-for-loop: 1.712

Swift-for-in-loop: 5.136

Swift-reduce: 4.961 -Os/ -O Objective-C-for-loop: 2.418

Objective-C-fast-enumeration: 1.210 Sort Simple Array Swift-for-loop: 1.712

Swift-for-in-loop: 5.136

Swift-reduce: 4.961 -Ofast Objective-C-for-loop: 2.418

Objective-C-fast-enumeration: 1.210