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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:16:46+00:00 2026-05-16T16:16:46+00:00

I am trying to detect if an array isn’t empty in order to be

  • 0

I am trying to detect if an array isn’t empty in order to be able to do a certain call.
I tried using if (![array ==nil]) however that doesn’t compile.
I’m sure there is a really easy explanation to this.

Update

If array is empty I want to do this:

array = [[NSMutableArray alloc]init];

If it has an object I want to do this:

array = [[userDefaults arrayForKey:@"MyFavorites"] mutableCopy];
  • 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-05-16T16:16:46+00:00Added an answer on May 16, 2026 at 4:16 pm

    If you declared it but did not assign anything to it at all:

    NSMutableArray *array;
    

    Then the array will be nil, meaning it isn’t there at all so you can’t say if it’s empty or not, so you can’t check anything.

    If you did assign something to it, and you want to find out if the existing array is empty or not, that would depend on how you created it first.

    • If the array was assigned from some convenience method, it’s autoreleased, so just do this:

      if ([array count] == 0) {
          array = [[NSMutableArray alloc] init];
      } else {
          array = [[userDefaults arrayForKey:@"MyFavorites"] mutableCopy];
      }
      
    • If the array was assigned from an init or copy method, or it was retained previously, store the count in a temporary variable, release the array and use the temporary variable to decide what to do:

      NSInteger count = [array count];
      [array release];
      
      if (count == 0) {
          array = [[NSMutableArray alloc] init];
      } else {
          array = [[userDefaults arrayForKey:@"MyFavorites"] mutableCopy];
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to detect when an array element is null, but is_null isn't doing
Trying to detect left click vs right click (without using jQuery!) and I have
I'm trying to detect and remove those fields that after a sync are still
I'm trying to detect which controller I'm using or what controller I'm on and
I'm trying to develop a bit of JavaScript that will detect the language of
I'm trying to create a really simple to do list using an array and
I am trying to detect if a jQuery plugin is available, I tried $.pluginName
I have a simple OpenGL program and trying to draw an instanced array that
I'm trying to make a small game using (free)GLUT. I know that it's old
I am trying detect when a textarea becomes full for creating pagination effect. Using

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.