开发者

Dumping contents of Array Obj-c to consoler

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 li开发者_JAVA技巧ke to print those values out and make sure the correct ones are going in. Any ideas?

Thanks


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);


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?


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 ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜