swift, a nova linguagem de programação da apple (cocoaheads sao paulo)
Post on 18-Jun-2015
800 Views
Preview:
DESCRIPTION
TRANSCRIPT
SwiftA nova linguagem de
programação da Apple
Juliana Chahoud
Juliana Chahoud
Apple anuncia nova linguagem de programação!
+- 20 dias de experiência com Swift
Características Swift
• Moderna, Rápida e Segura
• iOS 7 e 8
• OS X 10.9 e 10.10
Xcode 6 - Beta2
Projeto ObjC x Swift• Swift não possui arquivos header (.h)
• main.m não é mais necessário
X
Não é necessário criar um projeto para “experimentar” o Swift
Playground REPL (Read-Eval-Print-Loop)
Demo: REPL
Como praticar de fato
• Criar um cenário comum de um app simples, interagindo com Cocoa e Objective-C:
• Utilizar alguma API - NSURLConnection
• TableView e célula customizada
• Integração com Cocoapods e Obj-C
• Aproveitando a onda do momento: App Copa do Mundo
Estudo de caso: App Copa do Mundo
Criação do projeto WorldCup
Demo: app Copa do Mundo
https://github.com/jchahoud/WorldCup-Swift.git
AppDelegate/* In Obj-C: #import <UIKit/UIKit.h> */ import UIKit
/* "Replace" the main file and UIApplicationMain instance creation In Obj-C (in main.m): UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); */ @UIApplicationMain
/* In Obj-C: @interface AppDelegate : UIResponder <UIApplicationDelegate> */ class AppDelegate: UIResponder, UIApplicationDelegate {
/* In Obj-C: @property (strong, nonatomic) UIWindow *window; */ var window: UIWindow?
/* In Obj-C - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { */ func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { return true }
Storyboard Universal
API: resultado de jogos
Classe Match: Swift x Obj-Cimport Foundation
/* In Obj-C: @interface Match : NSObject */
class Match {
/* In Obj-C: @property (nonatomic,strong) NSString* c_AwayLogoImage; */ var c_AwayLogoImage:String?
var c_AwayTeam_en:String? var c_HomeLogoImage:String? var c_HomeTeam_en:String? var c_Stadium:String? var n_AwayGoals:Int? var n_HomeGoals:Int? /* In Obj-C: -(id) initWithDictionary:(NSDictionary*)dict { self = [super init]; if (self){ _c_AwayLogoImage = dict[@"c_AwayLogoImage"]; //.... } return self; } */ init(dict: NSDictionary!) { c_AwayLogoImage = dict["c_AwayLogoImage"] as? String c_AwayTeam_en = dict["c_AwayTeam_en"] as? String c_HomeLogoImage = dict["c_HomeLogoImage"] as? String c_HomeTeam_en = dict["c_HomeTeam_en"] as? String c_Stadium = dict["c_Stadium"] as? String n_AwayGoals = dict["n_AwayGoals"] as? Int n_HomeGoals = dict["n_HomeGoals"] as? Int
} }
Subclasse de UITableViewCellimport UIKit
class MatchCell: UITableViewCell {
@IBOutlet var homeTeam: UILabel @IBOutlet var homeGoals: UILabel @IBOutlet var homeLogoImage: UIImageView @IBOutlet var awayTeam: UILabel @IBOutlet var awayGoals: UILabel @IBOutlet var awayLogoImage: UIImageView @IBOutlet var stadiums: UILabel func configCell (match: Match)->Void { //load teams names and stadium homeTeam.text = match.c_HomeTeam_en awayTeam.text = match.c_AwayTeam_en stadiums.text = match.c_Stadium
//load team goals unwrapping the optionals if let goals = match.n_HomeGoals { homeGoals.text = "\(goals)" }
if let goals = match.n_AwayGoals { awayGoals.text = "\(goals)" } //lazy images loading with SDWebImage homeLogoImage.setImageWithURL(NSURL(string:match.c_HomeLogoImage))
awayLogoImage.setImageWithURL(NSURL(string:match.c_AwayLogoImage)) } }
API: informações de jogos let apiFifaUrl = NSURL (string: "http://live.mobileapp.fifa.com/api/wc/matches") var request:NSURLRequest = NSURLRequest (URL: apiFifaUrl) NSURLConnection.sendAsynchronousRequest (request, queue: NSOperationQueue.mainQueue(), completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
})
/* ... */
Integração com CocoaPods
Visualização 3D das camadas UI
Mais características do Swift
• deinit: chamado antes do objeto ser “deallocated”
• Gerenciamento de memória: Strong, Weak, e Unowned
• Propriedades:
• Podem ter getters e setters
• pode-se usar willSet e didSet
Mais características do Swift• Funções:
• podem ser aninhadas
• podem receber número variável de parâmetros (numbers: Int...)
• podem receber / retornar outras funções
• enum e struct podem ter funções
• extension (category) adiciona funcionalidade a um tipo existente
• Generics:
• func swapTwoValues<T>(inout a: T, inout b: T)
Mais Informações
Documentação
The Swift Programming Language Using Swift with Cocoa and Objective-C https://developer.apple.com/swift/
Vídeos
WWDC 2014 https://developer.apple.com/videos/wwdc/2014/
Sample Code https://developer.apple.com/library/prerelease/ios/navigation/
Obrigada! Dúvidas?
Juliana Chahoud
jchahoud@icloud.com @jchahoud
http://jchahoud.com
top related