Created
January 23, 2012 02:42
-
-
Save rudyjahchan/1660134 to your computer and use it in GitHub Desktop.
Monkey-Patching iOS with Objective-C Categories Part I: Simple Extensions and Overrides
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface AClass (ACategory) | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "AClass+ACategory.h" | |
@implementation AClass (ACategory) | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSDate+Formatting.h" | |
@implementation EntryCell | |
... | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSDate+Formatting.h" | |
#import "UIFonts+MyFonts.h" | |
#import "UIViewController+Tourbot.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 minutes ago |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSDate (Formatting) | |
- (NSString*) timeAgoInWords; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSDate+Formatting.h" | |
@implementation NSDate (Formatting) | |
- (NSString *)timeAgoInWords { | |
double seconds = [self timeIntervalSinceNow]; | |
seconds = seconds * -1; | |
if(seconds < 1) { | |
return @"now"; | |
} else if (seconds < 60) { | |
return @"less than a minute ago"; | |
} else { | |
NSUInteger difference = 0; | |
BOOL pluralize = NO; | |
NSString* unit = @""; | |
if (seconds < 3600) { | |
difference = round(seconds / 60); | |
unit = @"minute"; | |
} else if (seconds < 86400) { | |
difference = round(seconds / 3600); | |
unit = @"hour"; | |
} else if (seconds < 604800) { | |
difference = round(seconds / 86400); | |
unit = @"day"; | |
} else if (seconds < 2592000) { | |
difference = round(seconds / 604800); | |
unit = @"week"; | |
} else if (seconds < 31557600) { | |
difference = round(seconds / 2592000); | |
unit = @"month"; | |
} else { | |
difference = round(seconds / 2592000); | |
unit = @"year"; | |
} | |
if (difference > 1) { | |
pluralize = YES; | |
} | |
return [NSString stringWithFormat:@"%d %@%@ ago", | |
difference, | |
unit, | |
(pluralize ? @"s" : @"")]; | |
} | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSString*) description { | |
return [self timeAgoInWords]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Availability.h> | |
#ifndef __IPHONE_3_0 | |
#warning "This project uses features only available in iPhone SDK 3.0 and later." | |
#endif | |
#ifdef __OBJC__ | |
#import <UIKit/UIKit.h> | |
#import <Foundation/Foundation.h> | |
#import <QuartzCore/QuartzCore.h> | |
#import <CoreText/CoreText.h> | |
#import "Extensions.h" | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSDate* oldDate = [NSDate dateWithTimeIntervalSinceNow:180]; | |
NSLog(@"Date was %@", oldDate.timeAgoInWords); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment