+ All Categories
Home > Technology > Introducción a Swift - cyliconvalley

Introducción a Swift - cyliconvalley

Date post: 07-Aug-2015
Category:
Upload: jose-manuel-navarro
View: 33 times
Download: 0 times
Share this document with a friend
37
Swift
Transcript

Swift

Introducción a Swift

Presentado en Junio 2014 como una de las grandes revoluciones de iOS 8

Intenta facilitar la curva de aprendizaje de Objective-C (difícil) a la vez que introduce características modernas al lenguaje

Introducción a Swift

Introducción a Swift

Introducción a Swift

Introducción a Swift

Introducción a Swift

El runtime es compartido con Objective-

C, lo que permite que sean 100%

interoperables

Un app puede estar escrita en cualquier

combinación de Objective-C y Swift

Introducción a Swift

Swift es un lenguaje orientado a objetos, pero no es estricto.

También incluye características de lenguajes funcionales: funciones de primer orden, inmutabilidad, pattern matching, etc.

Introducción a Swift

Soporta inferencia de tipos, para evitar definir los mismos tipos una y otra vez

Utiliza ARC internamente para la gestión de memoria, aunque la mayoría de las veces es transparente al programador

Introducción a Swift

Y lo más importante, soporta emojis!!

Let’s play with code

https://github.com/jmnavarro/swift-tour

Optionals

It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement.

The billion dollar mistake

Opcionales

let numero: Int? // por defecto es nil

let numero: Int? = 6

Opcionales

Clases, protocolos y extensiones

Herencia

Protocolos

Extensiones de clases

Extensiones de protocolos

Extensiones de protocolos ++

Swift 2

Swift 2

Cambios importantes en el lenguaje

Sin compatibilidad hacia atrás

Cambios enfocados a mayor seguridad

del código

Guard

Guard Swift 1.x

let name: String? = computeName()

if name == nil {

return nil

}

let nick: String? = computeNick()

if nick == nil {

return nil

}

processUser(name!, nick!)

Guard Swift 2

guard let name: String? = computeName()

else {

return nil

}

guard let nick: String? = computeNick()

else {

return nil

}

processUser(name, nick)

List comprehension

List comprehension

Swift 1.x

for item in sequence {

if item != "" {

process(item)

}

}

List comprehension

Swift 2

for item in sequence where item != “” {

process(item)

}

API checking

API checking

Swift 2

if #available(iOS 8.1, *) {

}

Throws

Throws Swift 1.x

func process(a: Int, inout error: NSErrorPointer) -> Int { ... error = NSError(...)

} var error: NSError? = nil process(1, &error)

Throws Swift 2

func process(a: Int) throws -> Int

... throw NSError(...) }

do {

try process(1)

} catch { } try! process(1)

Defer

Defer Swift 1.x

let name: String? = computeName()

if name == nil { makeSomething() return nil

}

let nick: String? = computeNick()

if nick == nil {

makeSomething() return nil

}

processUser(name!, nick!)

Defer Swift 1.x

defer { makeSomething() }

let name: String? = computeName()

if name == nil { return nil

}

let nick: String? = computeNick()

if nick == nil {

return nil

}

processUser(name!, nick!)


Recommended