Re: NSError: why returned directly?
Re: NSError: why returned directly?
- Subject: Re: NSError: why returned directly?
- From: William Bumgarner <email@hidden>
- Date: Wed, 26 Apr 2006 10:33:07 -0700
On Apr 26, 2006, at 10:22 AM, Keith Ray wrote:
In some of my own code, I'm following a convention borrowed from
Python, which allows a method to return a "tuple" (a sort of
non-mutable array). Python's syntax also allows a concise multiple
assignment of the contents of the tuple.
aTuple = someObj.someMethod( arg, arg2 );
result = aTuple[0];
errorReturned = aTuple[1];
# or ...
result, errorReturned = someObj.someMethod( arg, arg2 );
Of course, it's much more verbose in objective c, but I put up with
that:
NSArray* returnVals = [ someObj.someMethod: arg otherArg: arg2 ];
SomeType* result = [ returnVals objectAtIndex: 0 ];
NSError* errorReturned = [ returnVals.objectAtIndex: 1 ];
Of course, this is subject to consideration of the types involved ( I
don't really want to "box" up non-object types just to stick them into
an NSArray ), performance required, and so on.
PyObjC actually does this automatically for error parameters and
other ** parameters. It is done consistently across all such
methods. For example, a PyObjC validation method can be written as:
def validatePrice_error_(self, value):
print ">>>> validatePrice_error_", value #.price
error = None
if value >= 0:
return True, value, error
errorString = u'Price cannot be negative'
userInfoDict = {NSLocalizedDescriptionKey: errorString}
error = NSError.alloc().initWithDomain_code_userInfo_(
ITEM_ERROR_DOMAIN,
ITEM_NEGATIVE_PRICE,
userInfoDict)
return False, value, error
For Objective-C code, I still recommend following the same pattern as
the AppKit and Foundation in APIs you are designing. It leads to a
greater level of consistency and less code -- the developer will
never have to convert between error handling models in their own code.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden