Swift, a nova linguagem de programação da Apple (CocoaHeads Sao Paulo)

julianachahoud 1,652 views 24 slides Jul 01, 2014
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

Swift, a nova linguagem de programação da Apple

Swift é a nova linguagem de programação da Apple, anunciada no começo de junho durante a WWDC. Nessa apresentação será dada uma introdução à linguagem, através de um estudo de caso de um aplicativo da Copa do Mundo. Nesse aplicativo será...


Slide Content

Swift
A 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-C
import 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 UITableViewCell
import 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
[email protected]
@jchahoud
http://jchahoud.com