Download - Swift School #1
![Page 1: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/1.jpg)
Swift Школа
Сергей Пронин Empatika
![Page 2: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/2.jpg)
План
• Интро
• Why Swift?
• Как подружить Objective-C и Swift?
![Page 3: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/3.jpg)
ИнтроСергей Пронин
Ведущий разработчик Empatika Ведущий разработчик App in the Air Преподаватель НИУ-ВШЭ @spronin [email protected]
![Page 4: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/4.jpg)
Why Swift?• Multiple return values
• Optionals -> Safety
• Playgrounds
• Extensions
• Powerful enums
• Sweet syntax
![Page 5: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/5.jpg)
var variable: Double = 55 variable = 12 let constant = 42 //implicit Int
let str = "Answer is \(constant)"
var ar: [Int] = [1, 2, 3] var dic: [String: AnyObject] = ["key": "value", "num": 123]
for n in ar { if n % 2 == 0 { println(n) } }
for (key, value) in dic { println("\(key) -> \(value)") }
![Page 6: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/6.jpg)
var optional: String? = "Hello?"
if let exists = optional { println(exists) }
if optional != nil { println(optional!) }
if optional?.hasSuffix("?") { println("somebody home?") }
![Page 7: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/7.jpg)
let lang = "swift" switch (lang) { case "swift": println("young") case let x where x.hasSuffix("#"): println("wat?") case "js", "css": println("web") default: println("java, is it you?") }
//add <break> to exit clause //add <fallthrough> to fall through
![Page 8: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/8.jpg)
for i in 0..<ar.count { //0...n = [0,n] println(ar[i]) }
for var i = 0; i < ar.count; i++ { println(ar[i]) }
do { fetchMessagesFromServer() sleep(5) } while true
while ar.count > 0 { ar.removeLast() }
![Page 9: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/9.jpg)
func confession(name: String) -> String { return "I miss \(name)" }
confession("my mom")
func fl(a: [AnyObject]) -> (first: AnyObject, last: AnyObject) { return (a[0], a[a.count-1]) }
var result = fl(ar) println(result.first) println(result.1)
![Page 10: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/10.jpg)
func params(numbers: Int…) -> Int { var sum = 0 for n in numbers { sum += n } return sum }
params(1, 1, 2, 3, 5, 8)
![Page 11: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/11.jpg)
func isEven(n: Int) -> Bool { return n % 2 == 0 }
func filter(a: [Int], check: Int -> Bool) -> [Int] { var result: [Int] = [] for n in a { if check(n) { result.append(n) } } return result }
filter(0...10, isEven)
![Page 12: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/12.jpg)
filter(0...10, { (item: Int) -> Bool in return item % 2 == 0 })
filter(0...100, { item in item % 10 == 0 })
filter(0...100, { $0 % 10 == 0 })
filter(0...100) { $0 % 10 == 0 }
![Page 13: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/13.jpg)
class Shape { var sides = 0 var name: String
init(name: String) { self.name = name }
func description() -> String { return "Sides -> \(sides)" } }
var shape = Shape(name: "ama shape") shape.description()
![Page 14: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/14.jpg)
class Triangle: Shape { var a, b, c: Int init(name: String, sides a: Int, _ b: Int, _ c: Int) { self.a = a self.b = b self.c = c super.init(name: name) self.sides = 3 } var perimeter: Int { get { return a + b + c } } override func description() -> String { return "Triangle with perimeter -> \(perimeter)" } }
var tr = Triangle(name: "Tr", sides: 5, 10, 20) tr.description
![Page 15: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/15.jpg)
extension Shape {
class func plusFunction() -> String { return "I’m a class function " }
}
Shape.plusFunction()
struct StructShape { //all the same code, actually static func plusFunction() -> String { return "Passed by value" } } //struct objects are being passed by value //class objects are being passed by reference
![Page 16: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/16.jpg)
enum Status: Int { case Undetermined, Success, Failure }
func sendRequest() -> Status { //networking magic return Status.Success }
var result = sendRequest() switch result { case .Success: println("success") case .Failure: println("failure") case .Undetermined: println("n/a") } result = Status.fromRaw(1)! //Success result.toRaw() //1
![Page 17: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/17.jpg)
protocol NetworkingDelegate { func request(request: NSURLRequest, completedWithResult result: NSData?) }
class ServerHelper { var delegate: NetworkingDelegate? func doRequest(request: NSURLRequest) { var data = NSData(contentsOfURL: request.URL) delegate?.request(request, completedWithResult: data) } }
class Controller: NetworkingDelegate { func request(request: NSURLRequest, completedWithResult result: NSData?) { println("magic") }
let helper = ServerHelper() helper.delegate = Controller() let url = NSURL(string: "http://empatika.com") helper.doRequest(NSURLRequest(URL: url)))
![Page 18: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/18.jpg)
Objective-C + Swift
![Page 19: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/19.jpg)
Готовимся
typedef enum { StatusSuccess, StatusFailure } Status
typedef NS_ENUM(NSInteger, Status) { StatusSuccess, StatusFailure };
![Page 20: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/20.jpg)
+ (id)sharedInstance { //your singleton magic }
+ (instancetype)sharedInstance { //singleton magic }
- (instancetype)init { //initial magic }
![Page 21: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/21.jpg)
• В существующей Objective-C codebase -> New File - Cocoa Touch Class -> Swift ->Configure Header
• В созданный Bridging Header импортируем все, что Swift должен видеть из Obj-C#import “MyAFAPIClient.h”
• Чтобы Obj-C видел из Swift импортируем#import “ProductModuleName-Swift.h”
• Открыть Swift-классы и протоколы через @objc@objc(Venue)class Venue {…}
![Page 22: Swift School #1](https://reader034.vdocuments.us/reader034/viewer/2022051515/554faf49b4c9057b298b4fd5/html5/thumbnails/22.jpg)
Перегнать модель или Controller на Swift
Начать новый проект на Swift с модели или
Controller-а