2

I looked for how to dump and array to the console I have mostly found:

for (id name in arrayStuff)
    NSLog (@"Array contents:  %d", name);

I've tried different formaters %d %@ %g etc. which does print different stuff, but not the values I'm 99% sure are being entered into the object and consequently the array. This doesn't seem to work, how would you know what to use as the formater?

I have an NSMutableArray with instance of an object containing one int and two doubles added to the array in each loop. I would like to print those values out and make sure the correct ones are going in. Any ideas?

Thanks

rd42
  • 3,584
  • 15
  • 56
  • 68

3 Answers3

3

Your format specifier is wrong. NSArrays contain objects, not ints, so you have to use the specifier for Objective-C objects %@:

for (id name in arrayStuff)
    NSLog(@"Array element: %@", name);

Or just:

NSLog(@"Array contents: %@", arrayStuff);
Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
1

You want to use the "%@" format specifier for printing objects.

To have the contents of your object displayed (the int & doubles), you need to implement the -description method in your object.

See What is the Objective-C equivalent for "toString()", for use with NSLog?

Community
  • 1
  • 1
David Gelhar
  • 27,873
  • 3
  • 67
  • 84
1

If you've created an Object type to hold the values, then use that object type in your for loop, and then use the getter to access the objects:

for (ObjectName name in arrayStuff) {
    NSLog(@"Array int: %d", name.myIntValue);
    NSLog(@"Array double: %f ...
}
Michael Kernahan
  • 1,442
  • 1
  • 13
  • 15