Skip to content

Instantly share code, notes, and snippets.

@JimRoepcke
Created May 8, 2014 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JimRoepcke/e725695e288b18aaea4b to your computer and use it in GitHub Desktop.
Save JimRoepcke/e725695e288b18aaea4b to your computer and use it in GitHub Desktop.
This is how I'd write the sample code rewritten using JRErr. I think it's clean and concise, and doesn't need any tricks. (Not tested, may have bugs ;))
- (BOOL)incrementBuildNumberInFile:(NSURL *)fileURL error:(NSError **)error
{
BOOL result = NO;
static NSString * const sErrorDescription = @"Unrecognized File Format";
static NSString * const sBuildNumberKey = @"BuildNumber";
NSError *localError = nil;
NSData *fileData = [NSData dataWithContentsOfURL:fileURL options:0 error:&localError];
if (fileData)
{
localError = nil;
NSMutableDictionary *fileDict = [NSPropertyListSerialization propertyListWithData:fileData options:NSPropertyListMutableContainers format:NULL error:&localError];
if (fileDict)
{
NSNumber *buildNumber = [fileDict objectForKey:sBuildNumberKey];
if (buildNumber)
{
if ([buildNumber isKindOfClass:[NSNumber class]])
{
buildNumber = @([buildNumber intValue] + 1);
fileDict[sBuildNumberKey] = buildNumber;
localError = nil;
fileData = [NSPropertyListSerialization dataWithPropertyList:fileDict format:NSPropertyListXMLFormat_v1_0 options:0 error:&localError];
if (fileData)
{
localError = nil;
result = [fileData writeToURL:fileURL options:NSDataWritingAtomic error:&localError];
}
}
else
{
NSString *errReason = @"BuildNumber isn't a Number";
NSDictionary *userInfo = @{NSLocalizedDescriptionKey: sErrorDescription, NSLocalizedFailureReasonErrorKey: errReason};
localError = [NSError errorWithDomain:@"MyClass" code:-1 userInfo:userInfo];
}
}
else
{
NSString *errReason = @"BuildNumber is missing";
NSDictionary *userInfo = @{NSLocalizedDescriptionKey:sErrorDescription, NSLocalizedFailureReasonErrorKey: errReason};
localError = [NSError errorWithDomain:@"MyClass" code:-1 userInfo:userInfo];
}
}
}
if (!result && localError && error)
{
NSLog(@"error: %@", localError);
*error = localError;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment