Runtime Error Handling

Whilst Objective C has try catch error handling it generally is not used as an exception is a programmer error which should be fixed in the code not at run time.

Read More

Run Time Errors Via The Console Window

Always keep the console window open when running (Menu > Run > Console) Objective C is a dynamic typed language so the compiler doesn’t know if functions you use are actually available at compile time, but an error will be generated when it runs.  ALWAYS have the console window open so you see any errors […]

Read More

Random General

There is not a class in Objective-C with a method for generating random numbers but C may be used.  E.g: rand(), srand(), random(), srandom() and arc4random(). arc4random() tends to be preferred as it does not require seeding (it automatically initializes itself). Examples: int RandomNumber = arc4random() % 3; //Get random number from 0 – 2 […]

Read More

Creating An Initialisation Function

Create your own initialise function //************************************** //************************************** //********** CLASS INITIALISE ********** //************************************** //************************************** //Override the default init in case our class is created using init instead of our initialse funciton (no need to declare in .h file as we’re overriding a superclass method) – (id)init { return [self initMyInitFunctionName:@”” Value1:0]; } //Our new init function: […]

Read More

Date and Time Examples

NSDate objects are immutable. //Define a date variable NSDate *dateCreated; //Initialise it: dateCreated = [[NSDate alloc] init]; Getting Date Values NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@”yyyy”]; int year = [[dateFormatter stringFromDate:[NSDate date]] intValue]; [dateFormatter setDateFormat:@”MM”]; int month = [[dateFormatter stringFromDate:[NSDate date]] intValue]; [dateFormatter setDateFormat:@”dd”]; int day = [[dateFormatter stringFromDate:[NSDate date]] intValue]; [dateFormatter setDateFormat:@”HH”]; […]

Read More

Memory General

Variable & Object Types BOOL Values:- YES, NO NSLog(@"The answer is %i", MyValue); char UInt8 int 32bits (currently unless the iPhone ever moves to a 64bit chipset).  If you want to future protect pointers for a 64bit system you can use NSInteger instead. NSLog(@"Value = %i", MyValue); Converting an int to an object: [MyArray addObject:[NSNumber […]

Read More

Creating A New Class

Create the new class Menu > File > New File > iPhone OS > Cocoa Touch> Objective-C class Subclass of: NSObject Name it with your class name Save it in a suitable directory within your project Setting Up The Class With Basic Properites Etc In the .h file #import <Foundation/Foundation.h> @interface MyClassName : NSObject //ClassName:SuperclassName […]

Read More

Null

In objective C nil is used: MyObject1 = nil; if (MyObject1 != nil) { … In objective C it is not a requirement to check an object is not nil as sending a message to nil is OK – it won't cause an error. Setting an object to nill ("myObject = nil;") destroys it.

Read More

Classes General

Objects are defined in classes and therefore these two terms are often used interchangeably.  Each object is actually an instance of a class. Creating A Class Object NSMutableArray *arrayInstance = [[NSMutableArray alloc] init; ‘alloc’ allocates the memory for the object and ‘init’ initialises it. Instance Methods Sent to instances of the class (objects). Calling: //Simple […]

Read More