Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8775533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:49:23+00:00 2026-06-13T18:49:23+00:00

I am creating an application on iPhone where I am taking code written and

  • 0

I am creating an application on iPhone where I am taking code written and Java and translating into Objective-C, as the app must work similarly in both Android and iPhone. Now problems of course are there are certain areas where the languages differ quite a bit, and can be difficult to implement just right.

One of the areas where it is of course different is you cannot create NSMutableArrays or NSMutableDictionaries which only accepts objects of a certain type. So when I am creating the getters and setters for these objects, I have been doing this, example below:

  -(void)setPerson:(NSMutableArray *)personList_p
    {
        BOOL bAllowed = YES;
        for(int i = 0; i < [personList_p count]; i++)
        {
            if(![[personList_p objectAtIndex:i] isKindOfClass:[Person class]])
            {
                bAllowed = NO;
                break;
            }
            else
            {
                bAllowed = YES;
            }
        }

        if(bAllowed)
        {
        personList_i = personList_p;
        }
        else
        {
        //Raise error here
        }
    }

Now originally I was using NSRaise Exception, but after doing reading up on it, it recommends against this as exception handling is resource intensive and should not be used for this type of situation. So NSError I read was a good solution, so I was reading up on this guide

http://www.cimgf.com/2008/04/04/cocoa-tutorial-using-nserror-to-great-effect/

My question is two fold, if I decide to use NSError should, is it best practice to declare the NSError object in the method, i.e like this

-(void)setPerson:(NSMutableArray *)personList_p : (NSError **)error

Or is it okay just to declare the error within the else statement above, like this

 NSError *error = nil;
         NSMutableDictionary* details = [NSMutableDictionary dictionary];
                [details setValue:@"Invalid object passed into Array, object must be of type Person." forKey:NSLocalizedDescriptionKey];
                error = [NSError errorWithDomain:@"Invalid Object" code:200 userInfo:details];
                NSLog(@"%@", [error localizedDescription]);

Secondly, in the guide I linked above, in the conclusion the author says

In this example, I am checking to see if the error is still nil after
my message call. If it is no longer nil I know that an error occurred
and that I need to display it to the user. Apple provides a built in
method to do this with a call to presentError: on the NSApplication
instance

and he uses this line of code here

[NSApp presentError:error];

This does not work for me, I do not get an autocomplete option with this code, just says use of undeclared identifier. Am I missing something obvious here?

Also, just to be sure, is the method I am using, the best method to go about ensuring the user sends in an array with the correct object types? Or is there another method which could be used which would achieve the same thing but in a more efficient manner.

Thanks in advance!!

EDIT:

Additional question, would it be such a bad idea to use NSException, as the problem with NSError is the app carries on, I am considering having it so if they do pass in an array with an invalid object, that it does just cause the application to crash and raise an exception. Is there any major reason why I shouldn’t do this??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:49:25+00:00Added an answer on June 13, 2026 at 6:49 pm

    Now originally I was using NSRaise Exception, but after doing reading
    up on it, it recommends against this as exception handling is resource
    intensive and should not be used for this type of situation. So
    NSError I read was a good solution, so I was reading up on this guide

    He does not actually say that raising exceptions is inherently bad or resource intensive, he argues that @try/catch is the wrong way to go about things. That, and Cocoa applications (even more so I’m Cocoa-Touch) should refrain from throwing exceptions whenever possible. Exceptions indicate undefined behavior, not simple errors.

    Secondly, in the guide I linked above, in the conclusion the author…
    uses this line of code here

    [NSApp presentError:error];
    

    This does not work for me, I do not get an autocomplete option with
    this code, just says use of undeclared identifier. Am I missing
    something obvious here?

    That is because NSApp is a concept only accessible to Cocoa(or Mac) applications. It represents a pointer to “the application itself”, and has some pretty neat functions associated with it. Of course, iOS has no parallel concept, so errors are traditionally presented in a UIAlertView.

    Additional question, would it be such a bad idea to use NSException,
    as the problem with NSError is the app carries on, I am considering
    having it so if they do pass in an array with an invalid object, that
    it does just cause the application to crash and raise an exception. Is
    there any major reason why I shouldn’t do this??

    It is generally a bad idea to use NSException for anything other than extremely serious logic (such as when a call is made to an index out of the bounds of an NSArray, or when a UITableView’s data source happens to be out of sync with what the table demands). If you absolutely must stop program execution, it’s much cleaner to use NSAssert() to throw conditional exceptions. And even then, setters are not the place to be throwing exceptions. It would be far easier to simply return nil when the condition fails, then have the caller check for such an outcome and handle it appropriately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Before my problem I was creating an app for iPhone and, after taking my
I'm creating an iPhone application which depends on storing photos the user took into
I am creating a simle application on iphone.I want to change iphone app to
I am creating a simle application on iphone. I am using self.editButtonItem....The code belw
I'm creating an iPhone application that requires a user entering information into a form,
I am creating an application for the iPhone which involves having more than one
I am creating a budget application in iPhone, to budget for personal incomes and
I have recently started creating an iPhone application using Appcelerator's Titanium. Since the application
I am creating an PhoneGap application for iPhone. When I am designing and testing
I'm struggling in creating a RTL UI in iPhone application. The framework doesn't seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.