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

Hello World 2016 - Swift 101

Hello World 2016 - Swift 101

Título: Swift 101
Autor: Miguel Duarte
Contacto: MiguelDuarte[at]icloud.com

Avatar for Hello World Tech Conference

Hello World Tech Conference

February 10, 2016
Tweet

More Decks by Hello World Tech Conference

Other Decks in Technology

Transcript

  1. Hello • Miguel Duarte • Developer no Hospital São João

    • Licenciado Engenharia Informática @ISEP • Mestrado Informática Médica @FMUP • Swift Programming Language
  2. Swift • Nova linguagem de programação • Desenvolvida pela Apple

    • Segura, moderna e poderosa • OpenSource • https://swift.org
  3. Swift Targets • Apple permite criar aplicações para • iPhone

    e iPad • Mac • Apple TV • Apple Watch • Open Source • Servidores Linux
  4. Swift Open Source • Grande Comunidade • Projetos • Perfect

    Framework — http://perfect.org • SwiftyGPIO — https://github.com/uraimo/SwiftyGPIO
  5. Swift Development • Mac Osx • Xcode — Playground •

    Qualquer browser Web • IBM Swift Sandbox — http://swiftlang.ng.bluemix.net
  6. Swift - Variáveis • Declaração de uma constante let •

    Declaração de variável var • Constante só pode ser definida uma única vez. • Variável pode ser redefinida e/ou alterada ao longo do tempo.
  7. Swift - Tipos de Dados • Explícito let str :

    String = "Uma String" let count : Int = 0 let π : Double = 3.14 let ok : Bool = true • Inferido let str = "Uma String" let count = 0 let π = 3.14 let ok = true
  8. Swift - Tipos de Dados • Caracteres especiais let π

    : Double = 3.14 let dog = "" var = "Ferrari" • Manipulação let a = 3, b = 5 let str = "\(a) x \(b) = \(a * b)" print(str)
  9. Swift - Array & Dictionary • Arrays let array =

    ["Top", true, 3] for item in array { print(item) } • Dictionaries let dictionary = ["Top": 3 , "Bottom" :3 , "Left" : 4, "Right" : 4 ] for (key, value) in dictionary { print("\(key) : \(value)") }
  10. Swift - Functions • Função sem parâmetros func sayHello(){ print("Hello")

    } sayHello() • Função com um parâmetro func sayHello(to:String) { print("Hello \(to)") } sayHello(“Miguel")
  11. Swift - Functions • Função sem parâmetros com retorno Double

    func π() -> Double { return 3.14 } π() • Função com um parâmetro e com retorno Double func circle(raio:Double)->Double { return 2 * π() * raio } circle(1.0) • Função com dois parâmetros retorno Double func rectangle(width:Double, height:Double)->Double { return (2 * width) + (2 * height) } rectangle(100, 20)
  12. Swift - Optionals • Representam valores que podem ser nil

    var opcionalInt: Int? //Inicializado a nil (sem valor) opcionalInt = 10 let opcionalIntConstante: Int? opcionalIntConstante = 10 let opcionalIntConstante: Int? = 10 • Podem ser parâmetros ou retorno de funções func findIndexOfString(string: String, array: [String]) -> Int? { for (index, value) in array.enumerate() { if value == string { return index } } return nil }
  13. Swift - Unwrapping Optionals • ”Extrair” os valores let nomes

    = ["João", "Pedro", "Maria", "Miguel"] if let indexValue = findIndexOfItem("Miguel", nomes) { print("Hello, \(nomes[indexValue])") } else { print("Não encontrado") }
  14. Swift - Classes class Carro { let marca:String let modelo:String

    let ano:Int var valorVenda:Double? required init(marca:String, modelo:String, ano:Int) { self.marca = marca self.modelo = modelo self.ano = ano } } var oMeuCarro = Carro(marca: "BMW", modelo: “i8", ano: 2004)