Friday 2 May 2014

Post WDI - iOS Development Device Deployed App (Memory Leaks, MVC, Custom Methods, Forward Declarations, Fluid Imaging, Animations, Sensors, Testing, Debugging, Deployment)

Activities @ TeamTreeHouse - 

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

DONE Building First iPhone App (Beyond the Basics)
  • Primative Data (Scalar) Types (Fixed memory size allocated by compiler depending on type automatically) { char %c, unsigned int %u, long %ld, float %f, double %lf }
  • Pointers (Dynamic Memory with variable storage) 
    • Memory Address %p  i.e. printf("%p", &i);
    • Mitigates "Passing by Value" Issues (instead of allocate mem twice for same data which is resource intensive, store value in only one place and pass pointer)  i.e. int i = 100; incrememt(int *x); int *i; // i outputs mem addr
  • Arrays
    • Inherit from NSObject (all objects do, it allows mem handling and access to runtime) creates list of string objects accessible with objectAtIndex
    • Example 1: int primts[3]; // store 3 no's. points mem addr 0 plus offset
    • Example 2: // equivalents
Alloc and Init Method
NSNumber *number; // declare varnumber = [NSNumber alloc]; // alloc mem add for object[number initWithInt:100]; // init mem w value using convenience method
Refactored EquivalentNSNumber *number = [[NSNumber alloc] initWithInt:100]; // equivalent to above three lines
Literal Shorthand Equivalent (iOS7 more readable and maintainable)NSNumber *oneHundred = @100; // alloc and init behind the scenes[NSNumber numberWithInt:100];
  • C Language Equivalents
  • Property Attributes (of Classes)
    • @  
      • Compiler Directive (highlights shorthand notation in Objective-C)
      • Examples: @"" (NSString), @100 (NSNumber), @[ ] (NSArray), @{ } (NSDictionary)
    • @property   (i.e. @property (nonatomic, strong) NSString *title;)
      • Uses: Declaration sets up getter and setter methods so can set and get instance vars encapsulated in custom classes, and so objects are available in all methods and may be used with 'self' instance of the View Controller (otherwise cannot get access as out of scope)  i.e. self.vehicles = [[NSArray alloc initWithObjects ...
      • Strong (reference) { retain object/property }
      • Weak (reference) { no concerned if underlying property deallocated and used for child in parent child relationships } 
      • Retain { used in previous versions before AFC when had to maintain retain counts)
      • AFC { Automatic Reference Counting)
      • Retain Count { retain count of 1 means only one object pointing to mem location (i.e. after alloc and init one custom class), whilst retain count of 2 means two objects pointing to same mem location until pointer set to nil }
      • Retain Count Logging { NSLog(@"Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef)title)); }
      • Deallocate { when no objects pointing to mem addr can deallocate }
      • Nonatomic { non multi-threaded apps where only one process reading or writing at any time }
      • Atomic { multi-threaded apps where multiple processes point to same mem locations and prioritiation of access to data or locking }
  • Memory Leaks
.h (header)@interface Vehicle : NSObject;@property (nonatomic, strong) id wheel;
.m (implementation)Vehicle *vehicle1 = [[Vehicle alloc] init];vehicle1.wheel = vehicle1; // 'wheel' is child property of vehicle1 pointing to its parent creating a 'retain' cyclevehicle1 = nil; // deallocated vehicle1 but 'wheel' is still pointing to vehicle1 and not released causing memory leak
  • Scoping and Self { declare delegates pointing to self when creating a delegate property. use 'self' to implement methods as part of Delegate Protocol }
  • Help and Sample Code { Help > Documentation and API Reference }
  • AppDelegate { helps hook into events of app lifecycle. not used for simple app (move to Support Files) }
  • Interface .h { defines methods and properties of class }
  • Implementation .m { implements methods of interface }
  • Communication and Connections { Code communicates with View Controller and Storyboard View elements connect with IBAction }
  • Target-Action Design Pattern { where target is View Controller and action is method created triggered by IBAction connection }
  • Shortcuts 
    • CMD+0 (Navigator show/hide)
    • CMD+R (Compiles program)
    • CMD+? (Help menu)
      • + .... <method>  { Class Methods that can be directly called on the Class }
      • - .... <method> { Instance Methods called directly on instance variables }
      • [ ...  { code for Method Calls / send a msg to Class }
      • ^{ }   { code denoted with carat denotes passing in a Code Block }
    • CTRL+Click+Drag (places controls relative to other control)
    • CMD+Click { shows class header info }
    • OPT+Click { shows info about class }
    • CMD+ ~ { switches between Xcode and Reference Docs }
  • UIView { belong to each View layer with a property NSArray of 'subViews' and contain added control elements within bounds }
  • Window { main View Controller attaches View to Window on launch of app }
  • UIViewController { manages resizing, layouts, interactions, events, coordination with data, lifecycle events upon load and unload of views }
    • viewDidLoad
    • viewWillAppear
    • viewWillDisappear
    • viewDidUnload
  • UIView Class { rectangular area defined by Frame property of type CGRect with size, origin, and Struct properties }
  • Randomisation { pseudo random numbers generator function arc4random_uniform(max_number) }
  • MVC (Model-View-Controller) Convention 
    • Benefits: design pattern following Single Responsibility Principle allows reuse of Model for multiple View Controllers, and custom models allow use of 'readonly' data to control format and prevent vulnerabilities
    • View (IB aka Storyboard)
    • Controller { View Controller class responsible for retrieving and displaying data on the View. Shift any data (i.e. NSArray) to the Model to achieve MVC design pattern }
    • Model { responsible for storing and providing data. data should be moved to its own class } 
  • Custom Getter and Setter Methods using Hidden Methods
    • Example
@property (strong, nonatomic) NSArray *vehicles; // declaration creates instance var with same name prefixed with underscore (hidden) providing access to this vars value using getters and setters
@interface LSCarYard:NSObject { // object with '_vehicles' as hidden instance var created by @property NSArray *_vehicles; }
// getter method (get is implied) would return an array object -(NSArray*) vehicles;
// setter method (takes Array object as argument) -(void) setVehicles: (NSArray*) newVehicles;
// alloc and initLSCarYard *caryard = [LSCarYard alloc] init];
// get object by calling the 'vehicles' method and returning the _vehicles instance variableNSArray *array = carYard.vehicles; // use dot notation to access object properties
// assign a new array setter by calling setVehicles method to set the value of _vehicles instance varcarYard.vehicles = array;
  • Forward Declarations { @class instructs header of class that a class exists but will be imported in the implementation file instead of the header (not good practice to import local classes within the @interface) }
  • Protect with ReadOnly Attribute (Data Traceability and Scalability)
    • Use: avoids overriding elements accidently (i.e. @property (strong, nonatomic, readonly) ...)
  • Images (images.xcassets)
    • Display Image Options
      • Image View Control 
      • UIImageView { insertSubview atIndex (i.e. background at 0) provides layer insertion flexibility versus just using 'addSubview' }
    • View Hierarchy drag-and-drop to modify layer in which they are displayed 
    • Device Displays 
      • Points Coordinate System { consistency across device resolutions }
      • Auto-Layout { responsive apps for different form factors }
      • Form Factor Toggle { 3.5 inch, 4 inch, device, etc } 
      • Constraints and Pins (Bridging and Validation) { points from bounds of screen to maintain height and width }
      • Fluid Resizing { fixes distorted image fill to fit flush }
      • Retina (2x) has 326 pixels per inch (PPI) for 3.5 inch display and is a high res image with high pixels in small area (not necessarily scaled)
    • Button States { normal (OFF), highlighted (ON), selected }
    • Relative Controls
      • Shortcuts {CTRL+Click+Drag (places controls relative to other control) }
  • Image Based Animation (rapid sequence of images measured in FPS creates illusion of motion)
    • UIImageView { array of UIImage objects (both normal and retina) for an animation }
  • Property Based Animation { using Alpha property for semi-transparency accompanied by animation code to create fading appearance }
  • Intercepting Motion Events (Animations)
    • Sensors 
      • Accelerometer (detects acceleration or speed)
      • Gyroscope (detects rotation events)
    • Events and Actions
      • Shake { captures acceleration and rotation along x, y, z, where constant for this motion is UIEventSubtypeMotionShake }
      • UIResponder is parent class and grandfathers methods including
        • motionBegan
        • motionEnded
        • motionCancelled
      • Simulate Motion Events { Menu > Hardware > <select gesture> }
  • Pragma Marks { directives that appear in Xcode menus to expedite access to methods }
    • #pragma mark - { used to categorise methods by creating a line between them in the Breadcrumb Navigation area }
  • DRY (Don't Repeat Yourself) Principle { refactor and organise code so repetitive tasks are shifted into and called from new methods to comply }
  • Sounds 
    • AudioToolBox Framework manual installation (does not automatically locate) into the Main Bundle using NSBundle path for resources to load sound
  • Testing, Debugging, and Device Deployment (built-in debugger)
    • Debug Navigator { compressing or decompressing the thread list with lower bar }
    • Debug Bar (toggle break points, step over and into)
    • Console
    • main.m (Supporting File in C)
    • Line Numbers (turning them ON)
    • Exception (Dynamic) Breakpoints 
    • Multiple Breakpoints (using Gutter)
    • UINib { View that the compiler creates from the Storyboard }
    • Orphaned Connections { IBOutlets in Connections Inspector no longer used }
  • App Icon and Launch Images 
    • Note: SystemUI automatically crops corners for display in Spotlight (first screen on iPhone when search for apps)
    • Points { standard size for non-retina iPhone, and 2x means twice the amount of points in pixels (i.e. 60 for 2x translates to 120x120 pixels) } 
  • App Store Configuration
    • Help > "app-related" (search)

No comments:

Post a Comment