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

  • Home
  • SEARCH
  • 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 9222981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:57:51+00:00 2026-06-18T03:57:51+00:00

Because of cocoa design patterns, the name of the instance method count in NSMutableArray

  • 0

Because of cocoa design patterns, the name of the instance method count in NSMutableArray is ambiguous; it could either return a saved variable count, or it could count the items in the array each time it’s called, and return that (cocoa dictates that a method that simply returns the value of a variable foo be foo, not getFoo). Does Objective C’s NSMutableArray actually count the items every time its count method is called, or does it return the value of a pre-calculated variable? If this were Java, I’d look at the source code, but I can’t find the source code for NSMutableArray. I looked at the API, and all it said was this:

Returns the number of objects currently in the array.

Since there was no context, I can’t tell if that means it does any calculations inside it or not.

  • 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-18T03:57:52+00:00Added an answer on June 18, 2026 at 3:57 am

    NSArray declares a private ivar _used that appears to hold the count of the array. Using object_getInstanceVariable() you can verify that in the case of the concrete subclass __NSArrayM, this variable is incremented and decremented as objects are added and removed.

    I used the following program, a Mac OS X Foundation command-line tool, to verify this:

    #import <Foundation/Foundation.h>
    #import <objc/runtime.h>
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
    
            NSMutableArray *anArray = [NSMutableArray array];
            NSLog(@"anArray's concrete class is %@", [anArray class]);
            UInt64 used = 0;
            for (NSString *toAdd in @[@"o", @"m", @"g"]) {
                object_getInstanceVariable(anArray, "_used", (void **)&used);
                NSLog(@"array = %@, used = %lld", anArray, used);
                [anArray addObject:toAdd];
            }
            for (NSString *toRemove in [anArray copy]) {
                object_getInstanceVariable(anArray, "_used", (void **)&used);
                NSLog(@"array = %@, used = %lld", anArray, used);
                [anArray removeObject:toRemove];
            }
            object_getInstanceVariable(anArray, "_used", (void **)&used);
            NSLog(@"array = %@, used = %lld", anArray, used);
        }
        return 0;
    }
    

    This program produced the following output:

    2013-01-31 17:40:15.376 Array[10173:303] anArray's concrete class is __NSArrayM
    2013-01-31 17:40:15.378 Array[10173:303] array = (
    ), used = 0
    2013-01-31 17:40:15.378 Array[10173:303] array = (
        o
    ), used = 1
    2013-01-31 17:40:15.379 Array[10173:303] array = (
        o,
        m
    ), used = 2
    2013-01-31 17:40:15.379 Array[10173:303] array = (
        o,
        m,
        g
    ), used = 3
    2013-01-31 17:40:15.380 Array[10173:303] array = (
        m,
        g
    ), used = 2
    2013-01-31 17:40:15.380 Array[10173:303] array = (
        g
    ), used = 1
    2013-01-31 17:40:15.380 Array[10173:303] array = (
    ), used = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read from the book Cocoa Design Patterns that the decorator pattern is
Because data from file look like this: line 1 is name (first last), next
Because this is my first attempt at an extension method that seems quite useful
I've mulled over Design Patterns for some time now and I am just starting
Does Cocoa provide a built-in method to convert a key string into a properly-formatted
I really hope someone can help on this because I'm learning cocoa and have
I'm working on an Objective-C/Cocoa roguelike, because I've always found that working on a
Every few months I try to re-learn Cocoa because I have no real use
I have a Cocoa/Objective-C class with a method that looks something like this: -
can someone give me a tutorial about key paths in Cocoa KVC concept, because

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.