objective-c primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include...

47
iOS Objective-C Primer

Upload: nguyenanh

Post on 24-Jul-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

iOSObjective-C Primer

Page 2: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

barnesandnoble.com

Page 3: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

barnesandnoble.com

Page 4: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}
Page 5: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}
Page 6: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

developer.apple.com

Page 7: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

iOS Dev Centerhttps://developer.apple.com/devcenter/ios/

Page 8: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

C

Page 9: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

#include  <stdio.h>

int  main(int  argc,  const  char  *  argv[]){        printf("Hello,  World!\n");        return  0;}

Page 10: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

statementsprintf("Hello,  World!\n");

Page 11: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

variablesint  n;

Page 12: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Primitive Data Typeschar

double

float

int

long

unsigned  int

...

Page 13: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

printf

%s

%d

%lu

%lld

%f

...

Page 14: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Boolean Expressions!    >    >=    ==    <=    <    &&    ||

Page 15: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Conditionsif    else

Page 16: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Loops

Page 17: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

for  (initialization;  condition;  increment)  {

       statements

}

Page 18: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

while  (condition)  {

       statements

}

Page 19: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

do  {

       statements

}  while  (condition);

Page 20: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Casting(  )

Page 21: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Pointers

char  *

double  *

float  *

int  *

long  *

...

Page 22: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

struct

Page 23: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

enum

Page 24: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Arrays[  ]

Page 25: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Memory Managementmalloc    free

Page 26: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Objective-C

Page 27: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Data Types

BOOL

id

nil

...

Page 28: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Foundation Data Types

NSInteger

NSPoint

NSRect

NSSize

NSUInteger

...

Page 29: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

.h

@interface  Foo:  NSObject  {

       //  instance  variables

}

//  declarations  of  methods

@end

Page 30: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

.m

@implementation  Foo

//  definitions  of  methods

@end

Page 31: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Instance Variables

@protected

@private

@public

Page 32: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Class Methods

+alloc;

Page 33: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Messages

Student  *student  =  [Student  alloc];

Page 34: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Instance Methods

-­‐  (int)age;

-­‐  (void)setAge:(int)age;

-­‐  (void)init;

-­‐  (void)initWithName:(NSString  *)name  andAge:(int)age;

Page 35: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Messages

[student  age];

[student  setAge:20];

[student  init];

[student  initWithName:@"Alice"  andAge:20];

Page 36: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Selectors

alloc

agesetAge:

initinitWithName:andAge:

Page 37: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

@propertyassigncopystrongweak

atomicnonatomic

readonlyreadwrite

Page 38: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

@synthesize

Page 39: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Collections

NSArrayNSMutableArray

NSDictionaryNSMutableDictionary

NSSetNSMutableSet

...

Page 40: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Fast Enumeration

for  (id  foo  in  bar)  {

       //  do  something  with  foo

}

Page 41: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Categories

@interface  Foo  (Bar)

-­‐(void)baz;

@end

Page 42: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Protocols

@interface  Student  <NSCopying>  {}

...

@end

Page 43: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

Protocols@implementation  Student

...

-­‐(id)copyWithZone:(NSZone  *)zone{        Student  *s  =  [Student  allocWithZone:zone];        [s  initWithName:_name  andAge:_age];        return  s;  }

...

@end

Page 44: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

NSException

@try  {

       //  try  something  here

}

@catch  (NSException  *e)  {

     //  handle  exception  here

}

@finally  {

       //  do  something  here}

Page 45: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

NSError

NSError  *e  =  nil;if  ([foo  bar:baz  error:&e]  ==  nil){        //  handle  error}

Page 46: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

MVC

http://developer.apple.com/library/ios/documentation/iphone/conceptual/iPhone101/Articles/02_DesignPatterns.html

Page 47: Objective-C Primercdn.cs76.net/2012/spring/lectures/7/lecture7-malan.pdf · #include intmain(intargc,constchar*argv[]) {printf("Hello,World!\n"); return0;}

to be continued...