iOS Hackathon 2012 Objective-C talk

29
OBJECTIVE-C AND YOU First step on your Journey to writing iOS apps Monday, January 14, 13

description

A primer on Objective-C and iPhone programming presented during December 2012 iOS Dev Scout Hackathon

Transcript of iOS Hackathon 2012 Objective-C talk

OBJECTIVE-C AND YOUFirst step on your Journey to writing iOS apps

Monday, January 14, 13

ABOUT

• Started Programming since 14 years old. iPhone development for 2+ years

• Runs software development company Getting Real - Clients include Prestige Magazine, Google Earth Hour etc..

• Does iOS Corporate Training - Clients include OCBC, Imerys, Barclays bank, IDA etc..

• Decided to go into mobile development because smartphones have changed and will continue to change the world

Monday, January 14, 13

WHY LEARN APP DEVELOPMENT?

• As of Jan 2013 - Apple has paid out of USD $7 billion to developers

• Apple now has over 200 million iTunes accounts - the largest credit card hub on the internet. Earning money online has never been easier

Monday, January 14, 13

PREREQUISITES

• An Intel-based Mac

• Xcode IDE

Monday, January 14, 13

OBJECTIVE-C BASICSAnd a random motorcycle.

Monday, January 14, 13

WHAT IS OBJECTIVE-C?

• A general purpose, high-level, object-oriented programming language that is a wrapper over C programming language

• The main programming language used by Apple for iOS programming

Monday, January 14, 13

WHY OBJECTIVE-C?

• Use Objective-C if you don’t want your app to suck. Seriously.

Monday, January 14, 13

OBJECT ORIENTATION

• Using objects as ‘data models’

• A way to think about programming

Monday, January 14, 13

VOCABULARY

• Class - The generic “type” of an object. e.g. Smartphones

• Instance - A specific object of a given class. e.g. iPhone 5

• Method - A function that an object performs. e.g. Make call, send message, surf for cat pictures on 9GAG

• Variable - A quantity or value. e.g. Percentage of battery life, number of messages in inbox.

Monday, January 14, 13

MORE VOCABULARY

• Class - Car

• Instance - BMW Z4

• Methods - Drive, convert roof, attract women

• Variables - Amount of fuel, speed (km/h)

Monday, January 14, 13

DATA TYPESName Explanation Example

void Nothing -

int Integer or number 1, 2, 3

float Decimal-point 3.14562

BOOL Yes or No YES, NO

NSString Sentence/ Word @”Hello World”

id Anything -

Monday, January 14, 13

DECLARING DATA TYPES

int cupsOfCoffee;int amountOfWorkfloat productivityLevel;

cupsOfCoffee = 3;productivityLevel = cupsOfCoffee * amountOfWork;

Monday, January 14, 13

DEFINING VARAIBLES

Class *objectName;- NSString *mySentence;

What is the * ?

Not for C types like int, float, BOOL

Monday, January 14, 13

METHODS

• A function that an object can perform

• Can return a value, or not

• e.g. the BMW class will have a getPrice method

- (int)getPrice:(Car *)typeOfCar {// Input code herereturn carPrice;

}

Monday, January 14, 13

METHODS

• Use Parameters / Arguments to pass more information to methods

• Example: squareNumber

- (int) squareMe: (int)x {return x * x;

}

[self squareMe:20];

Monday, January 14, 13

METHODS

• Methods with more than one parameter/ argument

• Example: multiplyNumbers

- (int) multiplyNumbers: (int)x andSecondNumber: (int)y{return x * y;

}

int numProduct = [self multiplyNumbers:10 andSecondNumber:50];

Monday, January 14, 13

METHODS

• Invoking Methods on Objects

[object method];[object methodWithArgument:argument];int variable = [object methodWithArgument:argument];float batteryLifeLeft = [iPhone getBatteryLife];

Monday, January 14, 13

LOOPS AND CONDITIONSMaking your code smart

Monday, January 14, 13

LOOPS

• Execute a piece of code multiple times

• Automate repetitive tasks

Monday, January 14, 13

FOR LOOP

• Running a ‘for’ loop

for ( initialization ; test condition ; increment) {// Code block

}

for ( int i = 0 ; i < 10 ; i++) {NSLog(@”Hello fellow iOS devs!”);

}

• This will print “Hello fellow iOS devs!” to the console 10 times

Monday, January 14, 13

WHILE LOOP

• Running a ‘while’ loop

while (condition is true) {// Execute code

}

BOOL isNetworkConnected = YES;while (isNetworkConnected) {[self downloadData];

}

• This will download data so long as the network is connected

Monday, January 14, 13

CONDITIONALS

• An important idea in programming is taking different actions depending on different circumstances

• Conditional expressions are always either true or false

• e.g. Is there coffee available in the pantry? If yes, make some coffee. If not, lament in despair (or just go out and buy some)

Monday, January 14, 13

IF-ELSE STATEMENTS

• Writing an if-else statement

int number = [self getNumberFromInput];

if (number > 9) {NSLog(@”Number is a double-digit number”);

} else if (number == 0) {NSLog(@”Number is not valid”);

} else {NSLog(@”Number is a single digit number”);

}

Monday, January 14, 13

COMMON CLASSES

• Introduction to a few commonly used classes

• Not exhaustive

Monday, January 14, 13

NSOBJECT

• NSObject is the base class that every object in Objective-C and Cocoa Touch inherits from

Monday, January 14, 13

NSSTRING

• NSString is an extremely commonly used class

• Type for ‘sentences’ or ‘words’

• NSString *myString = @”Hi There!”;

Monday, January 14, 13

NSARRAY

• Type for a collection or ‘array’ of objects grouped together

NSArray *weapons = [NSArray arrayWithObjects:@”rifle”, @”pistol”, @”plasma gun”, nil];

Weapon *equippedPistol = [weapons objectAtIndex:1];

• Remember in programming we always start counting from 0

Monday, January 14, 13

NSDICTIONARY

• Type for a collection of objects indexed using key-value pairs

NSDictionary *playerData = [NSDictionary dictionaryWithObjectsAndKeys:@”playerWeapons”, @”rifle”, @”highScore”, 9214, @”numberOfLivesLeft”, 3, nil];

int livesLeft = [playerData objectForKey:@”numberOfLivesLeft”];

Monday, January 14, 13

THANK YOU!

Gabriel LimGetting Real Software Development

www.gettingrail.com

Monday, January 14, 13