ios fundamentals with objectivec
TRANSCRIPT
iOS App Development
Day #1 : Fundamentals of Objective C
By Madusha Perera
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
Brad Cox : ObjC
Scott Forstall: iOS lead
Steve Jobs
Chris Lattner : Swift
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
Objective C - Data types
● int/double/float … (C primitives)
● NSInteger/NSUInteger (OC primitives)
● NSNumber
● NSString
● NSArray …
**NSLog() format specifiers … i,d,u,@
Pointers in ObjC
NSString *spell1 = @“abracadabra”
NSString *spell2 = spell1;
abracadabraspell1
spell2
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
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
weak :
The Obj can be destroyed independently. If that
happens the reference value automatically will
become nil.
Owner Obj Obj
nil
Low memory
warning !
nonatomic/atomic:
Time
Thread #1
Thread #2
Obj
setName
getName
nonatomic is faster
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];
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.
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
Interface (.h file)
@Interface NewClassname:ParentClass
<Protocol1>, <Protocol2> {
// private member variables}
//properties//public methods (instance or class methods)
@end
Implementation (.m file)
@implementation NewClassName
//method implementations@end
Protocols
Source:http://rypress.com/tutorials/objective-c/protocols
Protocols cont.
@protocol ProtocolName <NSObject>
@required//required method stubs
@optional//optional method stubs
@end
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
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
Design Patterns : Delegate
Design Pattern : A general reusable solution to
a commonly occurring problem.
Delegate Pattern
Delegate
Delegator
Where it all starts : main()
Hands control to the UIKit framework.
iOS App Development
Day #2 : Building an App with UI
Where it all starts : main()
Hands control to the UIKit framework.
Delegate Pattern in use
Architectural Pattern : MVC
Controller
Accept user input and react orchestrate
View
ModelBusiness Logic + Data
Core functionality and data
Display information to the user
States
Launched or
terminated
Running but not
receiving events
(phone call/sms)
Running in
background if
permits
Source:https://developer.apple.com
Source:https://developer.apple.com
IBOutlets and IBAction
IB : Interface Builder
Outlet and IBAction are just flags to tell the
xcode this is to do with the UI
Connecting the things
Lets create a small app ...