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 8246503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:31:32+00:00 2026-06-07T22:31:32+00:00

Does anyone know why NextStep/Apple decided to take the convenient method of doing nothing

  • 0

Does anyone know why NextStep/Apple decided to take the “convenient method” of doing nothing when passing a Nil object a message, but the “Java method” of raising an exception when passing an instantiated object an invalid selector?

For example,

// This does "nothing"
NSObject *object = Nil;
[object thisDoesNothing];

object = [[NSObject alloc] init];
// This causes an NSInvalidArgumentException to be raised
[object thisThrowsAnException];

So on one hand, we have the convenience of not having to check for Nil (assuming we don’t care too much about the result of the method call)–but on the other hand we have to check for an exception if our object doesn’t respond to a method?

If I’m not sure if the object will respond, I either have to:

@try {
    [object thisThrowsAnException];
} @catch (NSException *e){
    // do something different with object, since we can't call thisThrowsAnException
}

Or,

if([object respondsToSelector:@selector(thisThrowsAnException)]) {
    [object thisThrowsAnException];
}
else {
    // do something different with object, since we can't call thisThrowsAnException
}

(The latter is probably the better way to do it, since if object is Nil, the selector would NOT raise an exception, thus your code might not behave the way you want it to).

My question is:
WHY did Apple decide to implement it this way?
Why not have the unrecognized selector call to an instantiated object not raise an exception?
Alternatively, why not have the Nil object raise an exception if you try to call a method on it?

  • 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-07T22:31:33+00:00Added an answer on June 7, 2026 at 10:31 pm

    I can’t fully answer your question, but I can answer part of it. Objective-C allows you to send a message to nil because it makes code more elegant. You can read about this design decision here, and I will steal its example:

    Let’s say you want to get the last phone number that some person dialed on her office phone. If you can’t send messages to nil, you have to write it like this:

    Office *office = [somePerson office];
    // Person might not have an office, so check it...
    if (office) {
        Telephone *phone = [office telephone];
        // The office might not have a telephone, so check it...
        if (phone) {
            NSString *lastNumberDialed = [phone lastNumberDialed];
            // The phone might be brand new, so there might be no last-dialed-number...
            if (lastNumberDialed) {
                // Use the number, for example...
                [myTextField setText:lastNumberDialed];
            }
        }
    }
    

    Now suppose you can send messages to nil (and always get nil back):

    NSString *lastNumberDialed = [[[somePerson office] telephone] lastNumberDialed];
    if (lastNumberDialed) {
        [myTextField setText:lastNumberDialed];
    }
    

    As for why sending an unrecognized selector to an object raises an exception: I don’t know for sure. I suspect that it’s far more common for this to be a bug than to be harmless. In my code, I only want an unrecognized selector to be silently ignored when I need to send an optional protocol message (e.g. sending an optional message to a delegate). So I want the system to treat it as an error, and let me be explicit in the relatively rare case when I don’t want it to be an error.

    Note that you can tinker (to some extent) with the handling of unrecognized selectors in your own classes, in a few different ways. Take a look at the forwardingTargetForSelector:, forwardInvocation:, doesNotRecognizeSelector:, and resolveInstanceMethod: methods of NSObject.

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

Sidebar

Related Questions

Does anyone know how I could implement I predicate doing what this one does
Does anyone know why the last one doesn't work? object nullObj = null; short
Does anyone know how I can, in platform-independent C++ code prevent an object from
Does anyone know if there is a way to generate different code in the
Does anyone know of a free tool, similar to what is built into Visual
Does anyone know why UsernameExists wont return True. I must have my syntax messed
Does anyone know of a mechanism in Sybase ASA 9 / Sybase SQL Anywhere
Does anyone know of any websites, or (preferably) downloadable packages that you can use
Does anyone know of a way to contain a nonbreaking space in an html
Does anyone know how to make entity_id visible on the frontend as a sortable

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.