On Nov 2, 2009, at 22:18, Kyle Sluder wrote:

On Mon, Nov 2, 2009 at 10:06 PM, Roland King <r...@rols.org> wrote:
Haven't there been several threads recently telling people *never* to use the if( saveError ) check because saveError may be set even if the method
succeeds.

You can use it to determine if there is an error object available.  If
the method returns NO, it better return a valid NSError or set the
pointer to nil.

Just to clarify, since the earlier responses seem open to misinterpretation if not taken in context ...

There are two things NOT to do when using the standard error parameter pattern:

1. DO NOT initialize the NSError* variable to nil.

Doing so isn't literally harmful to the code, just a waste. The harm comes from thinking it does you any good.

1a. If the method call succeeds, the variable might nevertheless have been changed during the call. Its current value is trash.

1b. If the method call fails (returns nil or NO), the variable will of course have been changed to point to a valid object.

Either way, you never needed to initialize the variable.

2. DO NOT use the value of the NSError* variable unless the method call fails. See 1a, especially the part about "Its current value is trash."

I'm not aware of any frameworks methods that fail and pass back a nil value in the NSError* variable, although there may be such. I admit I've written some methods of my own that might pass back either a nil or non-nil NSError* value on failure, but I REALLY don't recommend it. The resulting code in the caller is lousy and incomprehensible.

So this is the correct pattern:

        NSError* error; // don't initialize to nil
        if (![someObject someMethodThatMightFail: ... error: &error]) {
                // Do something with the error object
// No need to test for nil, unless you have reason to really believe that // 'someMethodThatMightFail' might actually pass back a nil value on failure -- and you care if so
        }

At least, that's my understanding of what b.bum and others have been trying to beat into our heads for the last several times we've had this discussion.


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to