Saturday, 3 May 2014

Post WDI - iOS Development App (Master-Detail Table View, JSON, API, Mutable, Memory Management)

Activities @ TeamTreeHouse - 

iOS Development (using Xcode 5 IDE) with Amit Bijlani (Cont'd)

DONE Building API Reader (Table View) iPhone App
  • Table View (List) with Master-Detail App
    • Manual
    • Master-Detail Template
  • UIApplication { single instance, only allowed one UIApplication object for a single app. watches for Events and Routes to appropriate targets }
  • AppDelegate
    • Dfn: capability to Intercept Events at an App level so can Add Custom code. all apps have it to hook into UIApplication (app lifecycle). responsible to ensure app has a Window 
    • .h { defines lifecycle of app }
    • .m { implementation of .h containing methods }
  • UIWindow object { coordinates presentation of Views on screen. hosting Views and working with UIApplication object to deliver Events to Views and View Controller }
  • View Controller object { manages presentation of app Content on screen, including Single MainView (property) and its Collection of Sub Views  i.e. self.view (where View is property of ViewController) }
  • UIViewController class { base class for all ViewController objects, provides Default functionality for Views' ability to load, display, respond, etc }
  • View object { draws Content in designated Rect area and Responds to Events in that area }
  • Controls { specialised Views responsible for implementing interface objects (i.e. buttons, text fields, toggle switches) }
  • Data { ways to store data in app (i.e. Core Data, etc) }
  • Storyboard (defines flow between ViewControllers)
    • MasterViewController { inherits from UITableViewController and UIViewController as displays List of TableView UITableView with Rows(UITableViewCells) attributes and behaviour and where tap event shows DetailViewController }
    • DetailViewController  { inherits from UIViewController as displays Single View Text from Table View Cell }
    • Scene { ViewControllers }
    • Segue {  connection between Scenes of type UIStoryboardSegue (arrow Transition between each View Controller). it has Properties: identifier, sourceViewController, destinationViewController }
      • identifier { read-only shown in View. accessed in code with method prepareForSegue. use Method notation (instead of Dot Notation) when dealing with 'read-only' info as indicated in Help }
      • prepareForSegue { intercepts Segue once it is triggered and takes two args }
        • Segue param { contains info about Segue }
        • sender { originator of Segue }
(UIStoryboardSegue *)segue sender:(id)sender
  • Shortcuts
    • Double-Click - Zoom in/out of Storyboard canvas 
    • CMD+N - New File
  • UITableViewController { is declared in NSObjCRuntime.h (according to hover+CMD+Click) and implements two Protocols. It contains a @property 'tableView' of type UITableView which implements the two Protocols }
    • UITableViewDataSource { defines what is displayed in Table View. it mediates between Table View and Data }
    • UITableViewDelegate { defines how Data displayed with Methods to configure Table Views (i.e. row heights, section headers/footers, tap event or editing actions) }
@property (nonatomic, retain) UITableView *tableView;

  • indexPath { object with two Properties (Section, and Row in that Section) }
  • Memory Management
    • No Table View Cell Reuse { alloc and init new table view cell for each row to be displayed in table view. slow with large quantities of rows in the array to be alloc'ed }
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    • Efficient Reuse of Table View Cells { allows table view to efficiently manage cells with each one having an identifier in storyboard so when table view cell scrolls beyond screen the table view re-marks it for reuse and recycles it on new row }
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

  • NSDictionary 
    • objectForKey { type 'id' is generic data type (any Object) }
    • valueForKey { always expects String }
// declaration for key value pair NSDictionary *blogPost1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Blogpost1", @"title", @"Luke Schoen", @"author", nil]; 
// ALTERNATIVE convenience constructor for NSDictionary NSDictionary *blogPost1 = [NSDictionary dictionaryWithObjectsAndKeys:@"Blogpost1", @"title",@"Luke Schoen", @"author", nil];

  • SubTitles { Storyboard > Change Type (of TableViewCell) in Attribute Inspector > subTitle }
  • UITableViewCell Properties:
    • textLabel
    • detailTextLabel
  • Mutable 
    • Memory Inefficency { performance overhead when want to modify Contents of object, mem alloc needs to change and find mem space for additional Content }
    • NSMutableArray (subclass of NSArray)
NSMutableArray *titles = [NSMutableArray arrayWithCapacity:0]; // if capacity 0, use below NSMutableArray *titles = [NSMutableArray array]; // memory alloc minimal to hold pointer to array [titles addObject:@“My Book”]; // add object to array. mem dynamically alloc. array size grows [titles insertObject:@“abc” atIndex:0];  [titles removeLastObject]; [titles removeObjectAtIndex:0]; 
    • NSMutableDictionary (subclass of NSDictionary)
      • Declare with Memory Capacity
NSMutableDictionary *book = [NSMutableDictionary dictionary]; [book setObject:@“My Book”, forKey:@”title”]; [book setObject:@“Luke Schoen”, forKey:@”author"]; [book removeObjectForKey:@“title”]; [book removeAllObjects]; // removes all objects from a dictionary
    • NSMutableString (subclass of NSString) 
NSMutableString *string = [NSMutableString string];[string appendString:@"Title: My Book"];[string appendString:@"Author: Luke Schoen"];[string insertString:@" -- " atIndex:10]; 
  • Immutable { cannot be modified } 
    • Memory Efficient { when variable for object alloc, mem req'd known (knows qty objects and kind of objects. pass around without need to modify }
    • NSArray
  • JSON Parsing (JavaScript Object Notation)
// create NSURL class object (convenience class to breakdown url) 
// download the data (NSData object stream from JSON)   
NSURL *movieURL = [NSURL URLWithString:@""]; 
// parse data  
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL]; 
// declare pointer in memory 
NSError *error = nil; 
// deserialise data into JSON object format to stored in dictionary 
// pass pointer reference to NSError so value can be modified by the method it is being passed to (otherwise just passing by value and can only read 
// i.e. &__ means value can be modified by the method 
// i.e. ___ means just passing its value and it can only read your object NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; NSLog(@"%@", dataDictionary);
moviePosts = [dataDictionary objectForKey:@"Search”];

// extract 'title' from NSDictionary and set
cell.textLabel.text = [moviePost valueForKey:@"Title"];
cell.detailTextLabel.text = [
moviePost valueForKey:@"Year"];

Events Upcoming
    • ATTENDED - RORO Syd Dev Hub - 6:30pm 6th May 2014 (Level 15, 2 Market St)
    • UNABLE - RORO USer Group - 7pm 13th May 2014 (Trinity Bar, 505 Crown St Surry Hills)
    • UNABLE - Sydney Drupal Meetup - 7pm, 15th May 2014 (Level 5, 48 Pirrama Rd)

No comments:

Post a Comment