-1

Possible Duplicate:
NSMutableArray addObject not working

I have a NSMutableArray declared as (nonatomic,retain) in my .h file, and properly synthesized. tempRandomWord is an NSString that is always populated.However,count is always returning 0. Any ideas why?

[pastWords addObject:tempRandomWord];
int count=[pastWords count];
NSLog(@"%i",count);
Community
  • 1
  • 1
user1418214
  • 59
  • 2
  • 9
  • 1
    possible duplicate of [NSMutableArray addObject not working](http://stackoverflow.com/questions/1827058/nsmutablearray-addobject-not-working), [NSMutableArray addObject: not affecting count](http://stackoverflow.com/questions/3683761/nsmutablearray-addobject-not-affecting-count), [Cannot add items to an NSMutableArray ivar](http://stackoverflow.com/questions/7125326/cannot-add-items-to-an-nsmutablearray-ivar). – jscs Jul 20 '12 at 18:53
  • 4
    did you allocate `pastWords` anywhere? ie: `pastWords = [[NSMutableArray alloc] init];` – Dan F Jul 20 '12 at 18:53
  • You should be `NSLog`-ing `tempRandomWord` also. – Dustin Jul 20 '12 at 18:59
  • @Dan F- I did not, but I was under the impression that if I used ARC, that this is unnecessary for declared properties. I have other NSMutableArrays declared in the same way, without explicitly written allocation or initialization, that are working fine – user1418214 Jul 20 '12 at 19:01
  • @Dustin- I do, I just left it out of this question. It always comes up with a word – user1418214 Jul 20 '12 at 19:02
  • 3
    @user1418214 all arrays must be allocated and initialized in some way, ARC is irrelevant in that respect. All ARC does is you do not have to `retain` and `release` the properties manually. You must be assigning those other arrays to preconstructed arrays somehow. – Dan F Jul 20 '12 at 19:07
  • @DanF- that was it thanks! But how could I have gotten away with not allocating and initializing my other arrays that are being used without flaw? – user1418214 Jul 20 '12 at 19:17
  • @user1418214 It entirely depends on how you were assigning those arrays, without looking at more of your code, I cannot provide much of a better explanation – Dan F Jul 20 '12 at 19:24
  • If you don't allocate it, the variable supposed to hold the array will most likely be nil. Sending the message addObject: to nil will be ignored, and sending the message count will return 0. Sending messages to nil doesn't raise an exception, unlike calling methods on NULL in C++. – Nicolas Miari Jul 20 '12 at 19:31

2 Answers2

0

You may have forgotten to initialize your array. You need this line in your viewDidLoad or somewhere else

pastWords = [NSMutableArray array];

I personally like to put them where they're needed like so:

if (!pastWords) {
    pastWords = [NSMutableArray array];
}

[pastwords addObject:object];
Ryan Poolos
  • 18,421
  • 4
  • 65
  • 98
  • Please don't answer questions that clearly have duplicates indicated in the comments; instead, post your answer on the original. – jscs Jul 20 '12 at 19:47
0

Your array is not initialized. just add a lazy initialization somewhere before you read/use the property.

self.pastWords=self.pastWords?self.pastWords:[NSMutableArray array];

or if you use iVars directly:

pastWords=pastWords?pastWords:[NSMutableArray new];

or normal initialization in init

If you assign that property from outside of the class you may want to call mutableCopy to make sure the mutable array is used, even if NSArray is assigned.