2

I need a second pair of eyes on why I'm getting an error when trying to assign the NSError to the one passed into the function:

// Response and Error Objs.
NSURLResponse *response = nil;
NSError *requestError = nil;

// Attempt authentication
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];

// Error?
if (requestError != nil) {
    *error = requestError; // Error happens here
    return;
}
1
  • Maybe someone passed in nil? Where's the definition?
    – Carl Norum
    Oct 24, 2011 at 23:30

5 Answers 5

1

From Apple's Error Handling Programming Guide

Important: Success or failure is indicated by the return value of the method. Although Cocoa methods that indirectly return error objects in the Cocoa error domain are guaranteed to return such objects if the method indicates failure by directly returning nil or NO, you should always check that the return value is nil or NO before attempting to do anything with the NSError object.

In this case, -sendSynchronousRequest:returnResponse:error: returns an NSData object. You should check to see if that is nil before proceeding with the error.

0

Did you forget the NSError:

if (requestError != nil) {
    NSError *error = requestError; // Error happens here
    return;
}

Or where/when is error declared/defined and whats the point of assigning one error objet to another? Why not just:

if (requestError != nil) {
    NSLog(@"%@", [requestError localizedDescription]);
    return;
}
0

Most likely *error doesn't point to a valid pointer; the sample isn't complete, so I can't say for certain. Perhaps *error is nil.

0

you can't use 'error' in the form you are trying to use. You can access error in NSURLConnection but not outside it. Hence you need to define your error object before using it.

0

Lets say you have a method signature that accepts an NSError**.

This means that the method accepts a pointer to a pointer that is pointing to an NSError value.

So, I suspect because error is NULL, your basically asking that NULL object what the value is of the thing that it's pointing to. But of course, you cannot ask a NULL object for that information, that's why it crashes.

So, before you try to set the value of *error, you need to check that error is not NULL like so:

if (error == NULL) {
    // the function caller has supplied us with a nil as an argument
    // this indicates the caller does not care about the error
    return;
}

The reason you have to check for NULL instead of nil is because you are checking a memory location not a cocoa object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.