Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Stay hungry, stay foolish.

Stay hungry, stay foolish.

Introduction to iOS and Objective-C

weLaika

April 07, 2014
Tweet

More Decks by weLaika

Other Decks in Programming

Transcript

  1. NeXT • Jobs left Apple in 1985 • He created

    NeXT, Inc. • Computers and software for higher education • Commercial failure • Object-oriented operating system NeXTstep
  2. Back to Apple • NeXT stop building hardware in 1993

    • NeXT partnered with Sun to create OpenStep • Apple bought NeXT in 1996 • Apple used NeXTstep as foundation of Mac OS X (2001)
  3. iOS • iOS debuted with no name on January 2007

    • it became iPhone OS in 2008, with the first SDK • after the iPad, it was renamed to iOS in 2010 (v. 4.2)
  4. a Mac • a Mac is necessary to build native

    iOS App with Apple’s SDK • or you can make a web app, with first class support (http://goo.gl/dPMGNV)
  5. an iOS Developer account • free account, to download software

    and test in iOS Simulator • annual subscription, € 80/year to test on device and publish on the App Store
  6. SDK and Xcode • free download • Objective-C • web

    languages with PhoneGap • Ruby with RubyMotion • other languages…
  7. Xcode • app templates • test navigator • continuous integration

    on OS X Server • diagnostics (CPU, memory, energy use, …) • source control (git, Subversion) • debugger
  8. Objective-C - History • created by Brad Cox and Tom

    Love in the early ‘80s • reusability in software design • a C with objects, inspired by SmallTalk • NeXT licensed Objective-C from StepStone in 1988
  9. Objective-C ~ Messages • brackets • parts of the selector

    between the parameters • nesting (be careful!) • no type checking [myRectangle setWidth:16 andHeight:10]; myRectangle->setWidthAndHeight(16,10); [myRectangle setWidth:[container width] andHeight:10];
  10. // Rectangle.h #import <Foundation/Foundation.h> ! @interface Rectangle : NSObject {

    // Protected variables } ! @property (assign) NSInteger width; @property (assign) NSInteger height; ! - (void) setWidth:(NSInteger)w andHeight:(NSInteger)h; ! @end Objective-C ~ Interfaces
  11. Objective-C ~ Properties • setters and getters for free •

    property attributes @property (assign) NSInteger width; @synthesize width; [myRectangle setWidth:10]; NSInteger w = [myRectangle width]; NSInteger w = myRectangle.width;
  12. // Rectangle.m #import "Rectangle.h" ! @implementation Rectangle ! @synthesize width;

    @synthesize height = _height; ! - (id) init { self = [super init]; if (self) { width = 1; _height = 1; } return self; } ! - (void) setWidth:(NSInteger)w andHeight:(NSInteger)h { width = w; _height = h; } ! @end Objective-C ~ Implementations
  13. Objective-C ~ Instantiation • allocation of an uninitialized instance of

    the class • initialization • custom initializers Rectangle* myRectangle = [[Rectangle alloc] init]; Rectangle* myRectangle = [[Rectangle alloc] initWithWidth:10 andHeight:5];
  14. Objective-C ~ ARC • Automatic Reference Counting • compiler manages

    retain and release • objects are destructed as soon as they become unused • no background processing • can’t handle retain cycles Managed by compiler
  15. Objective-C ~ Categories • a way to add or replace

    methods of an existing class • full access to instance variables • use it carefully: just add small functionalities // Rectangle+Area.h ! # import "Rectangle.h" ! @interface Rectangle (Area) - (NSInteger) area; @end // Rectangle+Area.m ! # import "Rectangle+Area.h" ! @implementation Rectangle (Area) ! - (NSInteger) area { return self.width * self.height; } ! @end
  16. Objective-C ~ Protocols • multiple inheritance of specification • you

    can specify required and optional methods @protocol Geometry - (int) area; @end @interface Rectangle : NSObject <Geometry> { // Protected variables } ! …
  17. Objective-C ~ Delegation • a way to delegate some actions

    to another object • use of protocols • an object can be delegate for different objects and protocols @interface MyViewController : UIViewController <UITableViewDelegate> { UITableView* myTableView; } ! - (void)viewDidLoad { [super viewDidLoad]; myTableView.delegate = self; }