Skip to content

Category Archives: COCOA

Cocoa is one of Apple Inc.’s native object-oriented application program environments for the Mac OS X operating system. It is one of five major APIs available for Mac OS X; the others are Carbon, POSIX (for the BSD environment), X11 and Java.
Cocoa applications are typically developed using the development tools provided by Apple, specifically Xcode (formerly Project Builder) and Interface Builder, using the Objective-C language. – Wikipedia

Accelerometer setup and usage

To use the accelerometer in an iPhone application you need to add the <UIAccelerometerDelegate> into the .h file of your application: [code lang="c_mac"]@interface ApplicationViewController : UIViewController {[/code] Then in your .m file you must setup the accelerometer: [code lang="c_mac"] - (void)viewDidLoad { UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer]; accel.delegate = self; accel.updateInterval = 1/30; } [/code] [...]

Creating and using an Array of objects

First we need an array to hold the objects (in this case bullets) [code lang="c_mac"] NSMutableArray *myBullets = [[NSMutableArray alloc] init]; [/code] Create and then add the objects [code lang="c_mac"] for (int i=0; i

Changing the iPhone status bar

Just use this code to change the type of status bar shown. Stick it in the applicationDidFinishLaunching method which is usually in your MyAppDelegate.m [code lang="c_mac"] [application setStatusBarStyle:UIStatusBarStyleBlackOpaque]; [/code]

Round a number

To round a number use the following code: [code lang="c_mac"] CGFloat variable = ... CGFloat result = round(variable * 1000.0) / 1000.0; [/code] or [code lang="c_mac"] CGFloat variable = round(variable * 1000.0) / 1000.0; [/code] Use 1000 for 3decimal places, 100 for 2 etc!

Objective C Timer

This is a simple timer, you may need to declare some variables in the .h file. [code lang="c_mac"] -(void)onViewDidLoad { //set on start up NSDate *startTime = [NSDate date]; } [/code] [code lang="c_mac"] -(void)someOtherFunction { //amount to dely by (seconds) NSInteger *delayAmount = 1; //work out the current delay time NSTimeInterval elapsedTime = [startTime timeIntervalSinceNow]; [...]

String to Number (integer)

To convert a string of a number (“568″) to a integer (568): [code lang="c_mac"] //aString = "568" NSInteger *aNSintegerNumber = [aString integerValue]; //aNumber =568 [/code] or [code lang="c_mac"] //aString = "568" int *aIntNumber = [aString intValue]; //aIntNumber = 568 [/code]