<<

Xcode Coding Concepts! Object Oriented Programming 2 Classes 3 Class Structure Simplified 3 Special Types of Classes 3 Variables 5 Variable Types 5 Variable Data Types 5 Instance Variables between Classes 5 Methods 6 Instance Method 6 Class Method 6 Properties 8 Properties between Classes 8 Conditions 9 Evaluation 9 If statement 9 Switch statement 9 For loop 9 Do While loop 10 While loop 10 Data Storage 11 Array 11 Dictionary 11 Other 12 Screen Size 12 Storyboard Design 12 Object Oriented Programming! Application - a bunch of Objects working together Classes - are a blue print and defines components of the application - defines the type of object eg. car - generates instances of classes known as objects - contains properties, variables and methods Variables - declared in classes - used to store data or other objects Methods - declared in classes - provides your object with logic, perform tasks and functions - can be called by itself or other objects Properties - declared in classes - attribute of the class - eg. if a gearBox is the class then a property could be its dimensions or material SubClassing - inherits all properties and methods of parent class - create extra properties to sub class Classes! - like a blueprint and it has properties and behaviours. - creates instances of classes called objects. - the type of an object is referred to as a Class. - Objects have to be created using alloc] init] - Classes may include variables, properties and methods Class Structure Simplified! The following table does not include more complex structures Class & Syntax Notes

Header .h file // like a table of contents #import #import “OtherClass.h” // nested Classes listed here, optional @interface ClassName : ParentClass // defines the Class and its parent type { // define instance variables here // optional } // define properties here // optional // declare public methods here // optional, also called instance Methods @end

Interface .m file // contains the code/actions

#import “OtherClass.h” // linked Classes listed here @interface ClassName ( ) { // define instance variables here // optonal } @ end @implementation ClassName // public and private methods // methods coded here // define and use local variables in same method @end // instance variables used in any method Special Types of Classes! App Delegate class! - The entry point into the application. - Contains .h and .m files. ViewController class! - Contains the storyboard, .h and .m files which form the view controller. - A lot of the work in building iOS apps is making view controllers. - The job of a view controller is to manage a single screen from your app. One of the design principles of iOS is that each screen in your app gets its own view controller. Main.Storyboard (ViewController View)! - Is considered a view of the View Controller and describes the views and app flow. - Is managed by one or more View Controllers.

Variables! Variable Types! 1. Local variable exists within a method 2. Instance variable exists within the object but can be linked to other objects Class

.h file @interface Class () { variableType instanceVariableName; // instanceVariable created } // used in any method below

.m file -(void)methodName { variableType localVariableName; // localVariable created and restricted to current methodName // code // code may use local and instance variables } Variable Data Types! When defining a variable it can be: - created - created and assigned a value Booleaneg. BOOL var1 = YES; Integer eg. int var2 = 3; Floating Pointeg. float var3 = 3.9; String eg. NSString *variableName; eg. NSString *variableName = @“text”; Class eg. customClass *variableName;// creates a variable for customClass Instance Variables between Classes! Class A Class B

.h file .h file

#import “ClassB.m” -(returnType)methodName;

.m file .m file // can now use _instanceVariable here varType _instanceVariable; // declared -(returnType)methodName { // method goes here } Methods! Methods are actions or tasks that you want to execute They occur in .m files They are a of code. Can add objects to the view. methods can be public (.h .m) or private (.m only). Private can be access via self keyword Simple Method -(returnDataType) nameOfMethod// this line also appears in .h {// returnDataType - void, int, NSString* ClassName *objectName = [[ClassName alloc] init]; // creates a local variable objectName.property = @“Text”; // assigns a property to the object [objectName methodName]; // calls other method using the object return returnData;// required if returnDatatype not void } Method with Input -(returnDataType) nameOfMethod: (inputDatatype)inputName; // input called parameter eg.-(int)addFive:(int)input { return input +5; } Instance Method! Class Notes

.h file @interface classsName : parentName -(returnType)methodName; // - sign means it is an Instance Method -(returnType)methodName: (inputType)parameter1; // can only be used for this instance @end

.m file @implementation className -(returnType)methodName; { // code return returnType; // optional based on returnType } @end Class Method! Class Notes

.h file @interface classsName : parentName +(returnType)methodName; // + sign means it is a Class Method +(returnType)methodName: (inputType)parameterOne; @end

.m file Class Notes @implementation className +(returnType)methodName; { // code return returnType; // optional based on returnType } @end Properties! Properties are attributes of an Object. (confirm this) Declare in the .h file @property(attributes) IBType DataType *propertyName;// DataType = ClassType = type of object // attributes = strong, nonatomic // IB type is optional eg. @property(strong, nonatomic) NSString *titleText; Properties between Classes! Class Notes

.h file #import @interface ClassName : ParentClass; @property (attribute1, attribute2) dataType // attributes are optional propertyName; @end

.m file -(dataType) methodName { self.propertyName = value; // accessed using the self keyword } // used here to assign a value Conditions! Evaluation! Number or Boolean Evaluation! Equals To== Greater Than> Less Than< Not Equal To!= String Evaluation! Equals To [stringVariable isEqualToString:@“textString”] If statement! An If statement lets you test an expression for true or false and executes different branches of code depending on what conditions are met (or not met) Method Notes variableType variableName; // variables usually used to compare if (evaluationIsTrue) // Evaluation of Condition(s) { // code } else if (evaluationIsTrue) // else if is optional { //code } else // else is optional { //code // catches all other values } Switch statement! A switch statement tests a single int variable and executes different branches of code depending on what the int is. Method Notes variableType variableName; // variables usually used to compare switch (variableName) // may assign value here { case option1: // code break // Evaluation of Condition for option1 // code is optional case option2: // code break default: // code break // catches all other values } For loop! A For loop is a counted loop and lets you execute a set of code statements for a number of times until the count is met. Method Notes

for (startCondition ; finishCondition ; // start condition eg. int i = 0 incrementCounter) // finish condition eg. i < 10 { // increment counter eg. i ++ // code } Do While loop! A Do While loop will loop until a condition is met but is guaranteed to go through the loop at least once before checking the condition. Method Notes

variableType VariableName = value; // start condition based on variable // int num =0; do { // code // increment counter // increment counter eg. num=num+1; } while (conditionIsTrue); // finish condition eg. num <10; While loop! A While loop will loop until a condition is met but will check the condition at the beginning of each loop including the first. If the condition is not met initially, then the loop will not be executed. Method Notes

variableType VariableName = value; // start condition based on variable // int num =0; while (conditionIsTrue) // finish condition eg. num <10; { // code // increment counter // increment counter eg. num=num+1; } Data Storage! Array! Dictionary! Other! Screen Size! iPhone 3GS 320x480 iPhone 4/4S 640 x 960 http://www.appcoda.com/how-to-add-splash-screen-in-your-ios-app/ Documents links .m .h files and methods and how they all link etc list of all Object attributes e.g.. .text .color etc - and used for sub classing when to use UIView, UIViewTable etc - structure of how it all links Storyboard Design! Each screen has a View Controller Add buttons etc to each screen Add screens in the Storyboard and link via Segue. Link Screens by Right-click drag from button to another ViewController. Drag Storyboard cell (buttons etc) to .h file Creates a new property Displays and connects as IBOutlet Or Define the Interface Builder Action in the header file using -(IBAction) actionName: (objectType); Add the button to the Storyboard Right-click drag the button to the View Controller and link the IBAction Code the IBAction in the implementation file (.m) Drag Storyboard cell (buttons etc) to .m file Creates a new method Displays and connects as IBAction