Skip to content

Instantly share code, notes, and snippets.

@axelarge
Last active December 27, 2015 20:48
Show Gist options
  • Save axelarge/7386753 to your computer and use it in GitHub Desktop.
Save axelarge/7386753 to your computer and use it in GitHub Desktop.
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