Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 597
Simagen Avatar asked Dec 04 '25 12:12

Simagen


2 Answers

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

like image 89
Mick MacCallum Avatar answered Dec 06 '25 06:12

Mick MacCallum


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!

like image 44
WendiKidd Avatar answered Dec 06 '25 05:12

WendiKidd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!