Throwing an exception in iOS
February 26, 2013 in iOS Snippets
[NSException raise:@"Invalid parameter" format:@"Object %@ is incorrect", obj];
February 26, 2013 in iOS Snippets
[NSException raise:@"Invalid parameter" format:@"Object %@ is incorrect", obj];
February 16, 2013 in iOS Snippets
[[NSNotificationCenter defaultCenter] removeObserver:self];
February 16, 2013 in iOS Snippets
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
February 16, 2013 in iOS Snippets
Use this full-featured library: Reachability - an improved version of Apple’s classic Reachability code sample.
February 14, 2013 in iOS Snippets
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"640KB is not enough"
delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
[alert show]
February 14, 2013 in iOS Snippets
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
February 10, 2013 in iOS Snippets
NSString *className = NSStringFromClass([obj class]); Class class = NSClassFromString(className);
February 9, 2013 in iOS Snippets
[destinationObject setValuesForKeysWithDictionary: [sourceObject dictionaryWithValuesForKeys:[[NSArray alloc] initWithObjects:@"attr1", @"attr2", nil]]];
February 9, 2013 in iOS Snippets
+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size
{
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
February 9, 2013 in iOS Snippets
// [MYClass changeBrightness:someColor amount:1.1] returns a color that is 10% brighter than the original.
// 0.9 darkens by 10%. The percentage is relative to white, so 10% will lighten/darken the same amount
// regardless of how dark or light the color originally was. iOS5+
+ (UIColor*)changeBrightness:(UIColor*)color amount:(CGFloat)amount
{
CGFloat hue, saturation, brightness, alpha;
if ([color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]) {
brightness += (amount-1.0);
brightness = MAX(MIN(brightness, 1.0), 0.0);
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha];
}
CGFloat white;
if ([color getWhite:&white alpha:&alpha]) {
white += (amount-1.0);
white = MAX(MIN(white, 1.0), 0.0);
return [UIColor colorWithWhite:white alpha:alpha];
}
return nil;
}