iphone - Is there a convenient way to have two different "nil-like" possible return values from a method? -


the method in question (actually polymorphically-implemented family of recursive search methods) returns pointer type of object. had been returning nil in cases of failure. want have 2 different kinds of failure. easiest way implement that?

the best way keep return nil;, take error pointer 1 of function arguments. way, can send in error, , check value afterward see happened. function still run, you'll have control on gets returned.

as example, here's code use saving managedobjectcontext:

nsmanagedobjectcontext *moc = [self managedobjectcontext];  nserror *error; if (![moc save:&error]) {     nsstring *description = [error localizedescription];             nsinteger code = [error code]; } 

i've tweaked inside of block show couple of ways pull info out of error. you, it'd best use simple error codes distinguish between different nil values.

here's code example (though not best way, you'll have duplicate code interpret error messages):

-(id) functioncall:(nserror *)error {     ...     if (nilcondition) {          if (firstnilcondition) {             [error setcode:1];         }         if (firstnilcondition) {             [error setcode:2];         }          return nil;     } } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -