Upgrade to Pro — share decks privately, control downloads, hide ads and more …

diff objetive-c swift

R. Peres
September 27, 2014

diff objetive-c swift

A presentation I did about features missing in objective-c but present on Swift. This was done at NSPorto.

R. Peres

September 27, 2014
Tweet

More Decks by R. Peres

Other Decks in Programming

Transcript

  1. What would it be like if we had Objective-C without

    the baggage of C? — Craig Federighi
  2. In Swift: let multiplier3 = {(number : Int) -> Int

    in return number * 3 } let multiplier3 = {(number : Int) in return number * 3 } let multiplier3 = {(number : Int) in number * 3 } let multiplier3 = {number in number * 3 } let multiplier3 = { $0 * 3 }
  3. A quick example1: func until<L: BooleanType>(pred: @autoclosure ()->L, block: ()->())

    { while !pred() { block() } } And you would call it like this: var x = 0 until(x > 10) { x+=1 } 1 Example taken from http://airspeedvelocity.net/tag/auto_closure/
  4. Another use seen on the Swift library: func &&<T :

    BooleanType>(lhs: T, rhs: @autoclosure () -> Bool) -> Bool func &&<T : BooleanType, U : BooleanType>(lhs: T, rhs: @autoclosure () -> U) -> Bool
  5. With a var you can: var number = 1 number

    = 2 but not with let: let number = 1 number = 2 // error
  6. But you can go with the following: class Person {

    var name : String = "" } var juanito = Person() person.name = "Juanito" let juakin = Person() person.name = "Juakin" The juakin variable is immutable, but its property is not.
  7. Structs are passed by value, they are interchangeable, inert, predictable

    and controlled by a single owner. They can, but shouldn't create side effects. Classes are passed by reference, they can have multiple owners, they produce side effects (I/O, networking), they create state, they behave.
  8. Associated value enum Result { case Success(Person) case Error(String) }

    And then: let result : Result = .Success(Person()) switch result { case .Success(let person): doSomethingWithPerson(person) case .Error(let errorString): doSomethingWithError(errorString) }
  9. You could potentialy do the following: var person : Person?

    { get { switch (self) { case .Success(let person): return person case .Error: return nil } } }
  10. In a nutshell, optionals allows a variable to either have

    a value, or none (nil). — Me , just now
  11. For example(from Apple): let possibleNumber = "123" let convertedNumber =

    possibleNumber.toInt() // {Some 123} And: let possibleNumber = "Juakin" let convertedNumber = possibleNumber.toInt() // nil
  12. You can unwrap the value: let convertedNumber = possibleNumber.toInt() //

    {Some 123} let number = convertedNumber! // 123 Or: if let number = possibleNumber.toInt() { // Do something with it } else { // toInt() failed. :( }
  13. Tuples group multiple values into a single compound value. The

    values within a tuple can be of any type and do not have to be of the same type as each other. — The Swift Programming Language
  14. They are useful for temporary group of values, but they

    are not suitable for complex data structures or persistance.
  15. func weatherRequest() -> (weather: Weather?,error: String?) { // API request

    // Results parsed // Everything went great! var weather = Weather() return (weather,nil) } var weatherTuple = weatherRequest() switch weatherTuple { case let(.Some(weather),_): println(weather.description) case let(_,.Some(errorString)): println("error") default: println() }
  16. (...) is a style of computer programming in which algorithms

    are written in terms of types to-be-specified-later that are then instantiated when needed for specific types provided as parameters. — Wikipedia
  17. Generic code enables you to write flexible, reusable functions and

    types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner. --The Swift Programming Language
  18. Non-Generic: func findStringIndex(array: [String], valueToFind: String) -> Int? { //

    Body here } Generic: func findIndex<T>(array: [T], valueToFind: T) -> Int? { // Body here }
  19. func map<T, U>(x: T?, f: (T) -> U) -> U?

    { if let y = x { return f(y) } else { return nil } } T and U are placeholder types. It doesn't really matter what types they might be. The actual T and U will be determined when the map function is called.
  20. You can call this map function, as long as you

    respect the following: ▸ Pass a T that has the same type as the type that the function f receives. ▸ Return a U that has the same returning type as the function f.
  21. You can add other constrains: func findIndex<T: Equatable>... func contains<S

    : SequenceType where S.Generator.Element : Equatable>...
  22. Operator overloading func + (personA: Person, personB: Person) -> [Person]

    { return [personA,personB] } Custom Operator infix operator ||<- { associativity left precedence 160 } func ||<- (var lhs: [Person], rhs: Person) -> [Person] { lhs.append(rhs) return lhs } var morePeople = people ||<- p