Cocoa Cheat Sheet

Data Types

%@ Object
%hi short
%hu unsigned short
%d float
%u unsigned float
%f double
%i int

Example:

int mynumber = 1234;
NSLog (@"My number is:%i", mynumber);

Loops

int myNumber = 50;
int i = 1; while (i < myNumber +1) //Do this until i = myNumber(50)
	{
		NSLog(@"%i", i);
		i++; //increase by 1
	}

Tab View

Programatically switch to the first tab in a tab view.

- (IBAction)switchToTab1:(id)sender
{
	[tabView selectTabViewItemAtIndex:0];
}

Windows, Sheets and Panels

Shows an Alert panel.

NSRunAlertPanel(@"Alert, you have done something wrong", @"OK", @"Cancel",  NULL , NULL);

Show an Alert sheet
The myWindow is an outlet connected to the window you want the alert sheet to be attached too.

- (void)runAlertSheet
{
	NSBeginAlertSheet(@"You must be insane!", @"OK", @"Cancel",
		nil, myWindow, self, NULL,
		@selector(endAlertSheet:returnCode:contextInfo:),
		NULL,
		@"Are you sure you want to do such a reckless thing?");
}

- (void)endAlertSheet:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
	if (returnCode == NSAlertDefaultReturn) {
		NSLog(@"Clicked OK");
	}
	else if (returnCode == NSAlertAlternateReturn) {
		NSLog(@"Clicked Cancel");
	}
}

Bounce Icon in Dock

[NSApp requestUserAttention:NSCriticalRequest];

URL Encode a string

NSString *searchString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)@"keywords=hello world", NULL, NULL, kCFStringEncodingUTF8);