1

I was looking around and couldn't find anything, and I'm starting to think it's not possible with objective-c.

I have a NSMutableArray *myMutableArray and the size varies depending on what csv file is loaded. Since I do not set a size of myMutableArray I can't do:

       if (c == 5){

          myMutableArray[q] = [[NSNumber numberWithFloat:myOtherArray] stringValue];

          q = q + 1;
          c = 0;
       }

       Else {

           c = c + 1; 

       }

Since myMutableArray is technically of size nil I guess I can't add objects to it.

In cases, q can be between 1500 and 2500.

My question is, how do I make `myMutableArray' change size on every loop.

If this isn't possible, I guess I will have to make myMutableArray very large - but I need the values in myMutableArray for a graph. If I do myMutableArray = [[NSMutableArray alloc] initWithCapacity:5000]; and don't use the 5000 memory locations, will these locations be nil, or 0? (sorry if the technical words are wrong, by memory locations I mean the memory given to myMutableArray)

Thank you, if there is anything else I can add to this please feel free to let me know.

EDIT: What I'm trying to achieve is adding data to the array, and with that data create a graph

7
  • you better explain what exactly you need to achieve cause obviously you're going to do a wrong thing Commented Jan 21, 2014 at 13:47
  • and one more - Since myMutableArray is technically of size nil - it cannot be of size nil Commented Jan 21, 2014 at 13:48
  • @AndreyChernukha edited OP for what I'm trying to achieve Commented Jan 21, 2014 at 13:51
  • if you want just add objects to the array so what's wrong with simply using addObject: method? Commented Jan 21, 2014 at 13:53
  • Can I do that even if I don't set the array size? Commented Jan 21, 2014 at 13:54

2 Answers 2

2

You can't have a sporadically populated array. Creating an array with capacity 5000 is just a hint as to how much data you might be going to store into the array. Consider using an array of dictionaries where the dictionary contains the q value (presumably one coordinate) and the associated value. You can then sort the array based on the q values if you need to (for plotting). Then you can just add the dictionaries to the array as usual (addObject:).

Sign up to request clarification or add additional context in comments.

Comments

1

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray.

If you arrayWithCapacity: Creates and returns an NSMutableArray object with enough allocated memory to initially hold a given number of objects.

Mutable arrays expand as needed. When declaring them, we can init them like this:

+ (instancetype)arrayWithCapacity:(NSUInteger)numItems

Here numItems simply establishes the object’s initial capacity.

Later to add more data, i.e. to expand mutable array, use this

addObject:

What it does is, it inserts a given object at the end of the mutable array.

- (void)addObject:(id)anObject

It's important to note that:
The object to add to the end of the array's content. This value must not be nil. It raises an NSInvalidArgumentException if anObject is nil.

3 Comments

arrayWithCapacity: does not always allocate enough memory to hold the specified number of objects. That is used merely as a hint to configure the array. Since arrays can't have holes (empty slots), there is no need to consume all of said memory.
@bbum sorry but apple documentation for NSMutableArray class reference says otherwise. :(
Nope; the docs simply state establishes the initial capacity not that the array has allocated memory to hold all of that capacity. Array's backing store are not linearly allocated chunks of memory and the details change with the size of the array. This may be illuminating: ridiculousfish.com/blog/posts/array.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.