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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:24:01+00:00 2026-06-04T05:24:01+00:00

The way I understand it one of the things special about Objective C is

  • 0

The way I understand it one of the things special about Objective C is that you can send messages to NULL and it will just ignore them instead of crashing.

Why is it that NSArray doesn’t just return a NULL object if the index requested is out of bounds instead of causing a NSRangeException?

What I would expect from Objective C and NSArray is the following.

NSArray *array = [NSArray arrayWithObjects:@"Object 1", @"Object 2", @"Object 3", nil];

for (int i = 0; i < 5; i++) {
    NSString *string  = [array objectAtIndex:i];

    if (string) {
        NSLog(@"Object: %@",string);
    }
}

Allowing me to access indexes of the array with don’t contain objects and simply returning NULL. Then I can check if the object exists. I can do this in other places such as checking if the object has been instantiated.

NSArray *array;

if (!array) {
    array = [NSArray array];
}

I realize this is a theory based question but I’m curious 🙂

  • 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-04T05:24:02+00:00Added an answer on June 4, 2026 at 5:24 am

    Messages to nil is a language level feature, not a framework API feature.

    That NSArray (and other classes) throw an exception to indicate programmer error is a conscious design decision that was made when the API was designed (~1993). As both Kevin and Richard indicate, nil eats message is not erroneous in anyway whereas out of bounds is very much a case of erroneous input.

    Where would you draw the line? Should substringWithRange: accept any old input? How about setObject:atIndex:?

    By making NSArray @throw on bounds exceptions for indexOfObject: it makes the API consistent; any index (or range) that is out of bound will @throw.

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

Sidebar

Related Questions

One of the things that I don't understand about the Asset Pipeline in Rails
From what I understand there is no way of type-checking the messages send in
I am migrating an app to the High-Replication datastore. The way I understand that
The basic way that I understand Shiro's SecurityUtils.getSubject() to work is that it returns
I understand that the best way to develop a free and paid version of
Real-life case (helps understand the question) I am building a device that can freely
I am trying to understand a little more about the way rolling joins work
I'm learning about MVVM and one of the things I don't get is how
I understand how to design a database schema that has simple one-to-many relationships between
I'm not completely sure I understand the workflow way of doing things, but if

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.