views:

66

answers:

2

This is more a community sharing post than a real question. In my iPhone OS projects I'm always importing a helper class with helpful methods which I can use for about every project.

So I thought it might be a good idea, if everyone shares some of their favorite methods, which should have been in everyones toolcase.

I'll start with an extension of the NSString class, so I can make strings with dates on the fly providing format and locale. Maybe someone can find some need in this.

   @implementation NSString (DateHelper)

+(NSString *) stringWithDate:(NSDate*)date withFormat:(NSString *)format withLocaleIdent:(NSString*)localeString{ 
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    //For example @"de-DE", or @"en-US"
 NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeString];  
 [dateFormatter setLocale:locale];

    // For example @"HH:mm"
 [dateFormatter setDateFormat:format];  

 NSString *string = [dateFormatter stringFromDate:date];

 [dateFormatter release];
 [locale release];

 return string;  
} 
@end

I'd love to see some of your tools.

A: 
@implementation UIDevice (OrientationAddition)
- (UIInterfaceOrientation)vagueOrientation {

    if ([[UIApplication sharedApplication] statusBarFrame].size.height > 40){
        return UIInterfaceOrientationLandscapeLeft;
    }

    return UIInterfaceOrientationPortrait;
}
@end

I use it for determining orientation before anything else is called, also in this:

@implementation UIScreen (BoundsAddition)
- (CGRect)actualBounds {

    CGRect fakeBounds = self.bounds;
    if (UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] vagueOrientation])){
        return fakeBounds;
    }

    return CGRectMake(0, 0, fakeBounds.size.height, fakeBounds.size.width);
}
@end

Which returns bounds based on orientation.

Tom Irving
A: 
@implementation NSTimer (ECC)

-(void) suspend:(BOOL)inSuspend {
    [self setFireDate:inSuspend ? [NSDate distantFuture] : [NSDate dateWithTimeIntervalSinceNow:[self timeInterval]]];
}

@end
drawnonward