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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:06:41+00:00 2026-06-10T05:06:41+00:00

NSArray and NSMutableArray’s +arrayWithArray: returns empty array instead of nil when argument is nil.

  • 0

NSArray and NSMutableArray’s +arrayWithArray: returns empty array instead of nil when argument is nil.

    NSLog(@"%@", [[NSArray arrayWithArray:nil] class]);
    NSLog(@"%@", [[NSMutableArray arrayWithArray:nil] class]);

output:

    __NSArrayI
    __NSArrayM

But this behavior is not documented on Apples documentation.

Is it safe to rely on the assumption that arrayWithArray:nil returns empty array?
Or should I assign empty array explicitly like this:

    NSDictionary *dic = [[NSDictionary alloc] init];
    NSMutableArray *arr = [dic objectForKey:@"a"];
    if (!arr) {
        arr = [[NSMutableArray alloc] init];
    }
  • 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-10T05:06:43+00:00Added an answer on June 10, 2026 at 5:06 am

    The documentation of +arrayWithArray: says:

    Creates and returns an array containing the objects in another given
    array.

    Of course, nil is not an array, but [nil count] is valid and returns 0, so it might be treated as an empty array here.

    But I would not rely on that fact and create empty arrays with [[NSMutableArray alloc] init] or [NSMutableArray array].

    ADDED:

    If you call +arrayWithArray: with an invalid type, e.g. a NSString, then of course the program will throw an exception. But from the error message

    -[__NSCFConstantString count]: unrecognized selector sent to instance 0x69e4
    

    you can see that count is indeed the first method used to copy the array elements. That also explains why it works with nil.

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

Sidebar

Related Questions

NSMutableArray *Number=[NSArray arrayWithObjects: @1, @2, @3, @4, @5, @6, nil]; I have an array
I can declare NSMutableArray or NSArray but I want to declare class array. Let
NSMutableArray *output = [[NSMutableArray alloc]init]; or NSArray *output = [[NSMutableArray alloc]init]; I can set
NSArray arrayWithObjects needs nil at the end, NSString stringWithFormat and NSLog() doesn't. Why? [NSArray
-(NSArray*)getSomeStuff { NSMutableArray *array = [[NSMutableArray alloc]init]; // Add some objects to array return
How can I access @public NSArray or NSMutableArray after updating it? From one class
Short Question with a code example: NSLog(@%i, [[[NSArray alloc] init] retainCount]); NSLog(@%i, [[[NSMutableArray alloc]
I need to make an array, in which each object is a NSArray: NSMutableArray
What is the difference b/w NSArray and NSMutableArray ?
I have an NSMutableArray of NSArray's inside it. This is my current structure: Paused

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.