Created
September 3, 2018 10:13
-
-
Save dotnetguy83/4f0e56854a66129b874034f7c77be070 to your computer and use it in GitHub Desktop.
NSObject Time Elap Category (Objc)
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 "NSObject+Extensions.h" | |
#import <objc/runtime.h> | |
@implementation NSObject (Extensions) | |
- (void)tickWithName:(NSString *)name { | |
NSDate *startDate = objc_getAssociatedObject(self, CFBridgingRetain(name)); | |
if (startDate) { | |
NSLog(@"Tick (%@): nil", name); | |
return; | |
} | |
startDate = [NSDate date]; | |
objc_setAssociatedObject(self, CFBridgingRetain(name), startDate, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
NSLog(@"Tick (%@): %f", name, [startDate timeIntervalSinceNow]); | |
} | |
- (void)tockWithName:(NSString *)name { | |
NSDate *startDate = objc_getAssociatedObject(self, CFBridgingRetain(name)); | |
if (!startDate) { | |
NSLog(@"Tock (%@): nil", name); | |
return; | |
} | |
NSLog(@"Tock (%@): %f", name, [startDate timeIntervalSinceNow]); | |
objc_setAssociatedObject(self, CFBridgingRetain(name), nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
startDate = nil; | |
} | |
- (void)tickWithName:(NSString *)name LineTag:(NSString *)tag { | |
NSDate *startDate = objc_getAssociatedObject(self, CFBridgingRetain(name)); | |
if (!startDate) { | |
startDate = [NSDate date]; | |
NSLog(@"Tick (%@, %@): %f", name, tag, [startDate timeIntervalSinceNow]); | |
objc_setAssociatedObject(self, CFBridgingRetain(name), startDate, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
} | |
- (void)tockWithName:(NSString *)name LineTag:(NSString *)tag { | |
NSDate *startDate = objc_getAssociatedObject(self, CFBridgingRetain(name)); | |
if (startDate != nil) { | |
NSLog(@"Tock (%@, %@): %f", name, tag, [startDate timeIntervalSinceNow]); | |
objc_setAssociatedObject(self, CFBridgingRetain(name), nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
startDate = nil; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment