Last active
December 27, 2015 20:48
-
-
Save axelarge/7386753 to your computer and use it in GitHub Desktop.
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
typedef void(^LogBlock)(NSString *, ...); | |
#define LogBlock LogBlock NS_FORMAT_FUNCTION(1,2) | |
- (BOOL)loggingErrorsDo:(void (^)(LogBlock error))block | |
{ | |
__block BOOL success = YES; | |
block(^(NSString *messageFormat, ...) { | |
va_list args; | |
va_start(args, messageFormat); | |
NSString *message = [[NSString alloc] initWithFormat:messageFormat arguments:args]; | |
va_end(args); | |
DLog(@"%@", message); | |
success = NO; | |
}); | |
return success; | |
} | |
// Usage | |
- (BOOL)createAndSaveThingyWithName:(NSString *)name | |
{ | |
return [self loggingErrorsDo:^(LogBlock err) { | |
Thingy *thingy = [[Thingy alloc] initWithFluxCapacitor:self.fluxCapacitor]; | |
if (!thingy) return err(@"cannot create thingy with flux capacitor: %@", self.fluxCapacitor); | |
NSError *error; | |
if (![thingy setValue:name forProperty:kThingyNameProperty error:&error]) | |
return err(@"error setting thingy name %@: %@", name, error); | |
if (![self.repo addRecord:thingy error:&error]) | |
return err(@"error adding thingy %@ to repo: %@", thingy, error); | |
if (![self.repo saveAndReturnError:&error]) | |
return err(@"error saving added thingy %@: %@", thingy, error); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment