Skip to main content

How to use NSError in my iPhone App?



I am working on catching errors in my app, and I am looking into using NSError. I am slightly confused about how to use it, and how to populate it. Could someone provide an example on how I populate then use NSError?




Comments

  1. Well, what I usually do is have my methods that could error-out at runtime take a reference to a NSError pointer. If something does indeed go wrong in that method, I can populate the NSError reference with error data and return nil from the method.

    Example:

    - (id) endWorldHunger:(id)largeAmountsOfMonies error:(NSError**)error {
    // begin feeding the world's children...
    // it's all going well until....
    if (ohNoImOutOfMonies) {
    // sad, we can't solve world hunger, but we can let people know what went wrong!
    // init dictionary to be used to populate error object
    NSMutableDictionary* details = [NSMutableDictionary dictionary];
    [details setValue:@"ran out of money" forKey:NSLocalizedDescriptionKey];
    // populate the error object with the details
    *error = [NSError errorWithDomain:@"world" code:200 userInfo:details];
    // we couldn't feed the world's children...return nil..sniffle...sniffle
    return nil;
    }
    // wohoo! We fed the world's children. The world is now in lots of debt. But who cares?
    return YES;
    }


    We can then use the method like this. Don't even bother to inspect the error object unless the method returns nil:

    // initialize NSError object
    NSError* error = nil;
    // try to feed the world
    id yayOrNay = [self endWorldHunger:smallAmountsOfMonies error:&error];
    if (!yayOrNay) {
    // inspect error
    NSLog(@"%@", [error localizedDescription]);
    }
    // otherwise the world has been fed. Wow, your code must rock.


    We were able to access the error's localizedDescription because we set a value for NSLocalizedDescriptionKey.

    The best place for more information is Apple's documentation. It really is good.

    There is also a nice, simple tutorial on Cocoa Is My Girlfriend.

    ReplyDelete
  2. Great answer Alex. One potential issue is the NULL dereference. Apple's reference on Creating and Returning NSError objects

    ...
    [details setValue:@"ran out of money" forKey:NSLocalizedDescriptionKey];

    if (error != NULL) {
    // populate the error object with the details
    *error = [NSError errorWithDomain:@"world" code:200 userInfo:details];
    }
    // we couldn't feed the world's children...return nil..sniffle...sniffle
    return nil;
    ...

    ReplyDelete
  3. Please refer following tutorial

    i hope it will helpful for you but prior you have to read documentation of NSError

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.