ios fundamentals with objectivec

32
iOS App Development Day #1 : Fundamentals of Objective C By Madusha Perera

Upload: madusha-perera

Post on 15-Jul-2015

156 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Ios fundamentals with ObjectiveC

iOS App Development

Day #1 : Fundamentals of Objective C

By Madusha Perera

Page 2: Ios fundamentals with ObjectiveC

iOS and the technology

iOS - Mobile OS by Apple Inc (2007)

History

Steve Jobs worked in Apple

Left Apple and created Next

NeXTSTEP OS

After coming back to Apple initiated iOS

Page 3: Ios fundamentals with ObjectiveC

Brad Cox : ObjC

Scott Forstall: iOS lead

Steve Jobs

Chris Lattner : Swift

Page 4: Ios fundamentals with ObjectiveC

Layers of iOS

iPhone / iPad Hardware

UIKit.framework/M

apKit.famework/ ...

ImageIO.framewor

k/CoreVideo.fame

work/ ...

CoreData.framewo

rk/CFNetwork.fam

ework/ ...

Security.framewor

k/LibSystem ...

M

O

B

I

L

E

A

P

P

Page 5: Ios fundamentals with ObjectiveC

Objective C - Data types

● int/double/float … (C primitives)

● NSInteger/NSUInteger (OC primitives)

● NSNumber

● NSString

● NSArray …

**NSLog() format specifiers … i,d,u,@

Page 6: Ios fundamentals with ObjectiveC

Pointers in ObjC

NSString *spell1 = @“abracadabra”

NSString *spell2 = spell1;

abracadabraspell1

spell2

Page 7: Ios fundamentals with ObjectiveC

Properties in ObjC

A better way to define iVars.

@property(nonatomic,weak) NSString *name;

This automatically generates the following

_name

setName

getname (naming convention in ObC)

ARC

Page 8: Ios fundamentals with ObjectiveC

Property Attributes

strong,weak,atmoic,nonatomic, … etc

Strong:

The Obj will remain till Owner Obj lives in heap.

defaults are

in red

+1

Owner Obj Obj

Page 9: Ios fundamentals with ObjectiveC

weak :

The Obj can be destroyed independently. If that

happens the reference value automatically will

become nil.

Owner Obj Obj

nil

Low memory

warning !

Page 10: Ios fundamentals with ObjectiveC

nonatomic/atomic:

Time

Thread #1

Thread #2

Obj

setName

getName

nonatomic is faster

Page 11: Ios fundamentals with ObjectiveC

Methods are self documenting

Declaration-(void) driveForDistance:(double)theDistance atSpeed:(double)speed;

+ for class methods

- for instance methods

Calling methods

[myRemoteCar driveForDistance:20 atSpeed:5];

Page 12: Ios fundamentals with ObjectiveC

Classes and Objects

A Class defines what an object

will look like when its created.

(define the states and behaviors.)

Objects are self contained modules that can be

easily used, re-used as building blocks for a

software application.

Page 13: Ios fundamentals with ObjectiveC

Interfaces and Implementation

classes (.h and .m files)

An ObjC class is defined in terms of an

interface and an implementation.

class

.h

.m

API exposed to other classes

Actual implementation of class

Page 14: Ios fundamentals with ObjectiveC

Interface (.h file)

@Interface NewClassname:ParentClass

<Protocol1>, <Protocol2> {

// private member variables}

//properties//public methods (instance or class methods)

@end

Page 15: Ios fundamentals with ObjectiveC

Implementation (.m file)

@implementation NewClassName

//method implementations@end

Page 16: Ios fundamentals with ObjectiveC

Protocols

Source:http://rypress.com/tutorials/objective-c/protocols

Page 17: Ios fundamentals with ObjectiveC

Protocols cont.

@protocol ProtocolName <NSObject>

@required//required method stubs

@optional//optional method stubs

@end

Page 18: Ios fundamentals with ObjectiveC

Protocol cont.

// StreetLegal.h

#import <Foundation/Foundation.h>

@protocol StreetLegal <NSObject>

- (void)signalStop;

- (void)signalLeftTurn;

- (void)signalRightTurn;

@end

Source:http://rypress.com/tutorials/objective-c/protocols

Page 19: Ios fundamentals with ObjectiveC

Protocol cont.

// Bicycle.h

#import <Foundation/Foundation.h>

#import "StreetLegal.h"

@interface Bicycle : NSObject <StreetLegal>

- (void)startPedaling;

@end

// Bicycle.m

#import "Bicycle.h"

@implementation Bicycle

- (void)signalStop {

NSLog(@"Bending left arm downwards");

}

- (void)signalLeftTurn {

NSLog(@"Extending left arm outwards");

}

- (void)signalRightTurn {

NSLog(@"Bending left arm upwards");

}

- (void)startPedaling {

NSLog(@"Here we go!");

}

@end

Source:http://rypress.com/tutorials/objective-c/protocols

Page 20: Ios fundamentals with ObjectiveC

Design Patterns : Delegate

Design Pattern : A general reusable solution to

a commonly occurring problem.

Page 21: Ios fundamentals with ObjectiveC

Delegate Pattern

Delegate

Delegator

Page 22: Ios fundamentals with ObjectiveC

Where it all starts : main()

Hands control to the UIKit framework.

Page 23: Ios fundamentals with ObjectiveC

iOS App Development

Day #2 : Building an App with UI

Page 24: Ios fundamentals with ObjectiveC

Where it all starts : main()

Hands control to the UIKit framework.

Page 25: Ios fundamentals with ObjectiveC

Delegate Pattern in use

Page 26: Ios fundamentals with ObjectiveC
Page 27: Ios fundamentals with ObjectiveC

Architectural Pattern : MVC

Controller

Accept user input and react orchestrate

View

ModelBusiness Logic + Data

Core functionality and data

Display information to the user

Page 28: Ios fundamentals with ObjectiveC

States

Page 29: Ios fundamentals with ObjectiveC

Launched or

terminated

Running but not

receiving events

(phone call/sms)

Running in

background if

permits

Source:https://developer.apple.com

Page 30: Ios fundamentals with ObjectiveC

Source:https://developer.apple.com

Page 31: Ios fundamentals with ObjectiveC

IBOutlets and IBAction

IB : Interface Builder

Outlet and IBAction are just flags to tell the

xcode this is to do with the UI

Page 32: Ios fundamentals with ObjectiveC

Connecting the things

Lets create a small app ...