Home
Objective C - Programming Guide


Strings
NSString *str1 = @"hi";
NSMutableString *str2 = @"hi";
int len = [str1 length];
NSString *str1 = [NSString stringWithFormat:@"%d", c];
str2 = [str1 stringByAppendingString:@"hi"];
str2 = [str1 substringFromIndex:4];
str2 = [str1 substringToIndex:4];
str2 = [str1 substringWithRange:NSMakeRange(3,5)];
[str1 isEqualToString:@"hi"];
str2 = [str1 uppercaseString];
str2 = [str1 lowercaseString];
str2 = [str1 capitalizedString];

str2 = [array componentsJoinedbyString:@"|"];
if([str rangeOfString:substr].location != NSNotFound)
Arrays
NSMutableArray *strArray = [[NSMutableArray alloc] initWithObjects:@"hi", @"bye", nil];

[strArray addObject:@"new"];
[strArray insertObject:@"new" atIndex:0];
[array removeObjectAtIndex:0];
[array removeObject:@"hi"]; // removes first instance
[array removeObjectIdenticalTo:@"hi"]; // removes all of type
[array removeLastObject];
NSArray *words = [str1 componentsSeperatedByString:@" "];
str1 = [mArray objectAtIndex:0];

sorted = [array sortedArrayUsingSelector: @selector(localizedCaseInsensitiveCompare:)];
Loops
For(int i=1;i<=10;i++) { }
while(x<4) { }
do { x++; } while(x<4);
For(NSString *str1 in mArray) { }
Casting
int count = [str1 intValue];
float f2 = [str1 floatValue];
int count = (int)f2;
int c = round(f2);
NSString *str1 = [NSString stringWithFormat:@"%d", c];
Dates
NSDate *dDate = [NSDate date]; // sets to right now

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];

@"yyyy-MM-dd HH:mm:ss zzz" //<-- standard date/time format
@"hh:mm:ss a" //<-- time with am/pm
NSDate *dDate = [df dateFromString:@"2011-04-15 12:14:23"];

df.dateStyle = NSDateFormatterLongStyle;
NSString *dateF = [df stringFromDate:dDate];
[df release];

NSDate *newDate = [dDate dateByAddingTimeInterval:3600]; //<-- add one hour
Views
[self setTitle:@"Main Menu"];

TableView
[tableView setBackgroundColor:[UIColor blackColor]];
tableView.backgroundColor = [UIColor colorWithPatternImage:img];

Cells
cell.backgroundColor = [UIColor yellowColor];
cell.textLabel.text = @"hi";
cell.textColor = [UIColor redColor];
cell. textLabel .font = [UIFont fontWithName:@"Arial" size:28];
cell.image = img1;

color
[UIColor colorWithRed:0 green:0 blue:1 alpha:1]
Images
UIImage *img1 = [UIImage imageNamed:@"bg1.png"];
NSData *imgData = [managedObject valueForKey:@"image"]; // image data stored in core data
UIImage *img2 = [UIImage imageWithDate:imgData];
UIImageView *bg.image = img1;
CGSize size=img1.size;
CGFloat width = size.width;
CGFloat.height = size.height;

Move & Rotate imageview
image.center = CGPointMake(40, 40);
image.transform = CGAffineTransformMakeRotation((0.05)*M_PI);
Labels
L1.text = @"hi";
L1.font = [UIFont fontWithName:@"Arial" size:28];
L1.backgroundColor = [UIColor clearColor];
L1.textColor = [UIColor blackColor];
L1.shadowColor = [UIColor whiteColor];
L1.shadowOffset = CGSizeMake(1,1);
Alerts
UIAlertView *alert = [[UIAlertView alloc]

initWithTitle:@"Title"
message:@"message"
delegate:nil
cancelButtonTitle:@"ok"
otherButtonTitles:nil]
[alert show];
[alert release];
actionSheets
UIActionShhet *actionShhet = [[UIActionSheet alloc]
initWithTitle:@"title"
delegate:self
CancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Other", nil];
[actionSheet showInView:self.view];