From 16f0864f67dba787dec5390ab39d1c1bfe5b455a Mon Sep 17 00:00:00 2001 From: Jim Roepcke Date: Thu, 5 Nov 2015 17:17:26 -0500 Subject: [PATCH] Update 2013-10-14-nserror.md The first change is to check the result value before checking the error, as we would when calling an API that returns a value and an error in a trailing (NSError **) parameter. The second change is to show how to wrap an underlying NSError in a NSError with a custom domain and code. --- 2013-10-14-nserror.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/2013-10-14-nserror.md b/2013-10-14-nserror.md index 1e073b8e..846265b6 100644 --- a/2013-10-14-nserror.md +++ b/2013-10-14-nserror.md @@ -116,7 +116,7 @@ NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { - if (error) { + if (!data) { NSLog(@"%@", error); } else { // ... @@ -132,15 +132,16 @@ To pass an error to an `NSError **` parameter, do the following: ~~~{objective-c} - (BOOL)validateObject:(id)object - error:(NSError * __autoreleasing *)error + error:(NSError * __autoreleasing *)outError { - BOOL success = ...; + NSError *error = nil; + BOOL success = ...; // call an API passing &error into its (NSError **) parameter if (!success) { - if (error) { - *error = [NSError errorWithDomain:NSHipsterErrorDomain - code:-42 - userInfo:nil]; + if (outError) { + *outError = [NSError errorWithDomain:NSHipsterErrorDomain + code:-42 + userInfo:@{NSUnderlyingErrorKey: error}]; } }