Simagen
Simagen

Reputation: 419

what does the NSLog array count code shown do?

NSLog(@"%d",[annotations count]);

The above code is used with an NSMutableArray named 'annotations'.

My question is... what does this code do exactly?

I know NSLog outputs text, and its saying annotations count.. so I am thinking that it outputs the amounts of elements in the annotations array.

Am I correct?

Upvotes: 3

Views: 3816

Answers (3)

WendiKidd
WendiKidd

Reputation: 4373

Yes, you are correct exactly. NSLog prints text to the console window, and the count function of an array outputs the number of elements in that array.

NSLog prints text in a special way; wherever there is a %i, %d, %f, %@, etc, it replaces that symbol with a passed variable. So if I typed:

NSLog(@"Hi my name is %@. I am %i years old.", @"Sarah", 12);

The console would print:

Hi, my name is Sarah.  I am 12 years old.

So in your example, if annotations has 10 elements, 10 will simply be printed to the console. This can get confusing if you simply print a bunch of numbers though! So using NSLog's flexibility, it would be easier to read your log output if you did this:

NSLog(@"Elements in annotations array: %d", [annotations count]);

Then this would be printed to your console:

Elements in annotations array: 10

Which might be more helpful when reading back through your logs!

Upvotes: 2

Mick MacCallum
Mick MacCallum

Reputation: 130222

You could have just run the code to test this, but yes this command outputs to NSLog the count of the array named "annotations". For example if the array contains objects and indexes 0,1,2,3, and 4 the array's count will be 5.

NSArray * array = [NSArray arrayWithObjects:@"zero", @"one", @"two", @"three", @"four", nil];
NSLog(@"Number of items in my array is: %d", [array count]);//this will return (5)

For more details, see this post: Size of an NSArray

Upvotes: 3

Darren
Darren

Reputation: 10398

Yes it will simply display the count of objects in the array

Upvotes: 1

Related Questions