Wprowadzenie do języka Swift, czyli nowe podejście do programowania aplikacji dla iOS

26
Wprowadzenie do języka Swift

Transcript of Wprowadzenie do języka Swift, czyli nowe podejście do programowania aplikacji dla iOS

Wprowadzenie do języka Swift

Autor

Aleksander [email protected]

@AleksanderKania

Agenda

Zaszłości “historyczne” (Objective-C) Playground

Podstawowe konstrukcje Funkcje, closures, wyliczenia

Klasy, struktury, protokoły, metody Wbudowane kolekcje

danych

Objective-C - przykład

Swift - przykład

Dlaczego Swift?

Objective-C jest stary Objective-C jest trudny do nauki

Objective-C without C

Porównanie składni

Kompatybilność z istniejącymi już bibliotekami i frameworkami

Frameworki napisane w Objective-C można wykorzystać również w Swift

źródło: https://github.com/Alamofire/Alamofire

OS X vs iOS

NSTableView UITableView

AppKit UIKit

Witaj Świecie!

Objective-C

Swift

println(“Hello World”)

Demo - podstawowe konstrukcje

Podsumowanie

Czytelne i przydatne konstrukcje

..Kompatybilność wsteczna”

Nowoczesne podejście - połączenie zalet języka dynamicznego i silnie typowanego

Funkcje i closures

func convertStringToNumber(#number:String, callback: (String->Int))->Int{ var result:Int = callback(number) return result} func convert(num:String)->Int{

return num.toInt()!}

var result = convertStringToNumber(number: "4", convert)

var result = convertStringToNumber(number: "4", {(num:String) in return num.toInt()!})

Funkcje i closures

var result = convertStringToNumber(number: "4"){ (num:String) in return num.toInt()!}

var result = convertStringToNumber(number: "4"){ num in num.toInt()!}

Super easy!

var result5 = convertStringToNumber(number: "4"){ $0.toInt()!}

Wyliczenia

enum Number:Int{ case One = 1,Two,Three func description()->String{ switch(self) { case .One: return "one" case .Two: return "two" default: return String(self.rawValue) } }}let n = Number.Onen.description()

Struktury

Przykładowe struktury w Swift: Array, Set, Dictionary

struct Cat{ var avatar:UIImage?}

var cat = Cat(avatar: UIImage(named: "cat"))cat.avatar

Tablice i słowniki

var array:[String] = [String]()var numbers = Array<Int>()

array.append("Milk")numbers.append(14)

let constArray = [1,4,6,3]constArray.count

var dict:[Int:String] = [Int:String]()dict[4] = "Krowa"

dict[4]

var emptyDict = [:]

let constDict = [1 : "Krowa", 2 : "Mleko", 3 : "Milka", 4 : "Stek"]

Klasy

class Person:Details{ var firstName:String? var lastName:String? var age:Int? init(name:String, lastName:String, age:Int){ self.firstName = name self.lastName = lastName self.age = age } func displayDetails() { println("Człowiek nazywa się \(self.firstName) \(self.lastName) i ma \(self.age) lat.") } var fullName:String{ get{ return self.firstName! + " " + self.lastName! } }}

protocol Details{ func displayDetails()->Void}

Metody a funkcje

func foo(age:Int, val:Int){}

person.foo(45, 45)

func foo(age:Int, _ val:Int){}

Property observers

willSet - jest wywoływane przed zapisaniem wartości

didSet - jest wywoływane zaraz po tym jak nowa wartosć zostanie zapisana daje nam również dostęp do starej wartości!

Przydatne przy dziedziczeniu!

Property observers example

class StepCounter { var totalSteps: Int = 0 { willSet(newTotalSteps) { println("About to set totalSteps to \(newTotalSteps)") } didSet { if totalSteps > oldValue { println("Added \(totalSteps - oldValue) steps") } } } }

Q&A

Pytania?

Dzięki za uwagę

[email protected]