objective c - Defining a method that returns an error -


how define method returns error value?

for instance, when call managedobjectcontext save method, method returns boolean, along error:

if(![context save:&error]) {     nslog(@"%@", error); } 

would able give me straight-forward example of method definition behind this?

edit/update: in same regard, how possible pass multiple errors. i'm doing wrong below (i don't understand concept yet), doesn't work:

nsarray *errors = nil; [self throwmultipleerrors:&errors]; for(id error in errors) {     nslog(@"muliple error: %@", error); }...  -(bool)throwmultipleerrors:(nsmutablearray **) errors {     [*errors addobject:@"first error"];     [*errors addobject:@"second error"];     [*errors addobject:@"third error"];      return yes; } 

in example, method signature is:

- (bool)save:(nserror **)error; 

this allows caller pass in nserror address rather pointer (pointer pointer, ** , &), method can assign scope. , if method returns false, caller can check contents of error variable, shown in example.

see question more information on using & in front of variables , (nserror **) signatures.

why `&` (ampersand) put in front of method parameters?

example implementation:

    - (bool)save:(nserror **)error     {        nsassert(error != nil, @"passed nserror must nil!");        // attempt save        if (savedidfail) {              nsdictionary *userinfo = [nsdictionary dictionarywithobjectandkeys:@"save failed", kcustomerrorkey, nil];              *error = [nserror errorwithdomain:@"domain" code:999 userinfo:userinfo];              return no;        }         return yes;     } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -