Daniel G. Wilson
Daniel G. Wilson

Reputation: 15055

NSArray.count returns incorrect, giant integer

I have the following code, intended to select a random string from the array.

NSArray *popupMessages = [NSArray arrayWithObjects:
                          @"Shoulda' been bobbin' and weaving! Need anything from the shop?",
                          @"Don't forget you can use old boss's guns! Available in the shop!",
                          @"Hey Chaz, you Bojo! You need more POWER! Come by the shop for some better weapons!",
                          @"Aw… lame. Maybe I got something that can help you out here at my shop!",

                          nil];
int pmCount = popupMessages.count; // Breakpoint Here - pmCount = 971056545

int messageIndex = arc4random() % pmCount; // Breakpoint Here - same as above

I am using ARC with cocos2d. Any ideas as to why the array's count returns such a huge number? Thanks!

Upvotes: 0

Views: 774

Answers (3)

Carl Norum
Carl Norum

Reputation: 224864

Your problem just looks like it's a debugger artifact. It could be optimization-related, for example. Sometimes compilers can generate code that confuses debuggers pretty seriously. Add a log statement to make sure the debugger isn't just telling you lies.

Upvotes: 3

Abdullah Shafique
Abdullah Shafique

Reputation: 6918

Try:

NSInteger pmCount = [popupMessages count];

Upvotes: 0

Michael Dautermann
Michael Dautermann

Reputation: 89509

"count" is not a property, AFAIK.

The way I usually get the count for an array is:

[popupMessages count];

Upvotes: 0

Related Questions