32
Introdução ao Objective-C Thursday, June 13, 13

Introdução ao Objective-C

Embed Size (px)

Citation preview

Page 1: Introdução ao Objective-C

Introdução ao Objective-C

Thursday, June 13, 13

Page 2: Introdução ao Objective-C

Overview

Oriunda do C e do Smalltalk

Linguagem da NeXT para o NeXTSTEP OS

Orientado à objetos

Tipada

Thursday, June 13, 13

Page 3: Introdução ao Objective-C

Thursday, June 13, 13

Page 4: Introdução ao Objective-C

a linguagem

Thursday, June 13, 13

Page 5: Introdução ao Objective-C

non-ARC vs ARCAutomatic Reference Counting

Thursday, June 13, 13

Page 6: Introdução ao Objective-C

Interfaces (.h) and Implementations (.m)

Thursday, June 13, 13

Page 7: Introdução ao Objective-C

Interfaces (.h)

Thursday, June 13, 13

Page 8: Introdução ao Objective-C

Implementations (.m)

Thursday, June 13, 13

Page 9: Introdução ao Objective-C

@property

Objeto que nada mais é que uma propriedade de uma classe e que possui um getter e um setter.

Definição@property (nonatomic, strong) NSString *name;@property (nonatomic) int age;

Thursday, June 13, 13

Page 10: Introdução ao Objective-C

@property

@synthesize@implementation Ship

@synthesize name = _name;@synthesize age = _age;

// ... implemetações dos métodos- (BOOL)takeOff{ // ...}

@end

Thursday, June 13, 13

Page 11: Introdução ao Objective-C

@property

@synthesize name = _name é o mesmo que:@implementation Ship

// ... implemetações dos métodos- (BOOL)takeOff{ // ...}

- (NSString *)name{ return _name; // ou self.name (cuidado!)}

- (void)setName:(NSString *)name{ _name = name; // ou self.name = name (cuidado!)

}

@end

Thursday, June 13, 13

Page 12: Introdução ao Objective-C

Dot Notation

dado o objeto...@interface Ship

@property (nonatomic, strong) NSString *name;@property (nonatomic) int width;

@end

...podemos acessar suas properties:Ship *ship = [[Ship alloc] init];ship.name = @”Nave da Xuxa”;ship.width = 50;NSLog(@”%@ tem %d metros de largura”, ship.name, ship.width);

Thursday, June 13, 13

Page 13: Introdução ao Objective-C

strong vs weak

strong

matenha esse cara na heap até eu não apontar mais pra ele

weak

matenha enquanto alguém apontar pra ele “strongly”

Thursday, June 13, 13

Page 14: Introdução ao Objective-C

tipos primitivos

Herda os tipos primitivos do C

int

float

char

boolean

Thursday, June 13, 13

Page 15: Introdução ao Objective-C

nilÉ o valor de um objeto que não aponta para nadaid obj = nil;NSString *hello = nil;

É como o “zero” para um tipo primitivo

Pode ser testado num ifif (obj) { ... }

Pode enviar mensagens para nil (na maioria das vezes é ok)int i = [obj metodoQueRetornaUmInteiro] // se obj é nil, i = 0

Thursday, June 13, 13

Page 16: Introdução ao Objective-C

BOOL

typedef para o tipo boolean em Objective-Cif (flag) { ... }if (!flag) { ... }

YES e NOif (flag == YES) { ... }if (flag != NO) { ... }

Thursday, June 13, 13

Page 17: Introdução ao Objective-C

Instance vs class methods

de instância: definidos com um “-”- (BOOL)dropBomb:(Bomb *)bomb at:(CGPoint)position from:(double)altitude;

de classe: definidos com um “+”+ (Ship *)motherShip;

Thursday, June 13, 13

Page 18: Introdução ao Objective-C

Instance vs class methods

de instânciaShip *ship = [[Ship alloc] init];BOOL destroyed = [ship dropBomb:bomb1 at:dropPoint from:300.0];

de classeShip *motherShip = [Ship motherShip];

Thursday, June 13, 13

Page 19: Introdução ao Objective-C

Instance vs class methods

de instânciaself/super é a instância sendo chamada

de classeself/super é a classe

Thursday, June 13, 13

Page 20: Introdução ao Objective-C

Métodos

Mais exemplos- (double)performOperation:(NSString *)operation;

- (NSMutableArray *)operandStack;

- (NSString *)stringByAppendingString:(NSString *)s;

- (void)doSomething;

- (NSComparisonResult)compare:(NSString *)aString options:(Options)mask mask:(NSRange)range;

Thursday, June 13, 13

Page 21: Introdução ao Objective-C

Instâncias

alloc & initNSMutableArray *stack = [[NSMutableArray alloc] init];

alloc é um método de class

init é o construtor base

Thursday, June 13, 13

Page 22: Introdução ao Objective-C

Instâncias

Criando seu próprio init@implementation MyObject- (id)init{ self = [super init]; if (self) { // custom stuff } return self;}

@end

Thursday, June 13, 13

Page 23: Introdução ao Objective-C

Foundation Framework

Thursday, June 13, 13

Page 24: Introdução ao Objective-C

NSObject

- (NSString *)description;

- (id)copy;

- (id)mutableCopy;

Thursday, June 13, 13

Page 25: Introdução ao Objective-C

NSString

Serve como o tipo (char *) do C

Representada por @”minha string”NSString *minhaString = @”minha string”;

Imutável

Thursday, June 13, 13

Page 26: Introdução ao Objective-C

NSMutableString

Mutável :)

Porém raramente usada. Em geral, a instância de NSString é liberada no final do escopo- (void)fazAlgumaCoisa{ NSString *minhaString = @”minha string”; ...} // minhaString foi “released”

Thursday, June 13, 13

Page 27: Introdução ao Objective-C

NSArray

Coleção ordenada de objetos

Imutável (não consigo adicionar/remover objetos)

Pq é bom? Mais leve.

NSArray *meuArray = [[NSArray alloc] init];

NSArray *outroArray = [NSArray arrayWithObjects: @”Flamengo”, @”Vasco”, @“Botafogo”, @”Fluminense”, nil];

NSArray *array = @[@”Fla”, @“Flu”, @“Vasco”, @“Botafogo”];

Thursday, June 13, 13

Page 28: Introdução ao Objective-C

NSMutableArray

Mutável :)

Possui todos os métodos de NSArray

Mais pesado

NSMutableArray *array = @[@”Fla”, @“Flu”, @“Vasco”, @“Botafogo”];[array addObject:@”Friburguense”];[array removeLastObject];[array insertObject:meuObjeto atIndex:3];

array[1] = @”Madureira”;

Thursday, June 13, 13

Page 29: Introdução ao Objective-C

NSDictionary

Mapeamento chave-valor

Imutável

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:27], @”idade”, nil];

NSDictionary *dict = @{ @”idade”: @27};

Thursday, June 13, 13

Page 30: Introdução ao Objective-C

NSMutableDictionary

Mutável :)

NSMutableDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:27], @”idade”, nil];

[dict setObject:@”Gustavo” forKey:@”nome”];

dict[@”nome”] = @”Gustavo”;

Thursday, June 13, 13

Page 31: Introdução ao Objective-C

Outras classes

NSSet / NSMutableSet

NSOrderedSet / NSMutableOrderedSet

NSNumber

NSValue

NSData

NSDate

Thursday, June 13, 13

Page 32: Introdução ao Objective-C

NSObrigado

Thursday, June 13, 13