Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 2, 2009, 01:42 PM   #1
OldGit
macrumors newbie
 
Join Date: Feb 2009
Newbie: Print contents of array to console

It's been a while since i programed mac last, how to I print an arbitrary array in human readable form like say var_dump does in php?

Or any other way of visualizing arrays?
OldGit is offline   0 Reply With Quote
Old Feb 2, 2009, 02:32 PM   #2
toddburch
macrumors 6502a
 
Join Date: Dec 2006
Location: Katy, Texas
Send a message via AIM to toddburch Send a message via MSN to toddburch
Language?
toddburch is offline   0 Reply With Quote
Old Feb 2, 2009, 03:10 PM   #3
OldGit
Thread Starter
macrumors newbie
 
Join Date: Feb 2009
Quote:
Originally Posted by toddburch View Post
Language?
cocoa, objective c.

I sometimes forget there is any other method
OldGit is offline   0 Reply With Quote
Old Feb 2, 2009, 04:55 PM   #4
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Code:
// one way
NSLog(@"array: %@", array);

// a second way
for (id obj in array)
    NSLog(@"obj: %@", obj);
kainjow is offline   0 Reply With Quote
Old Feb 2, 2009, 05:05 PM   #5
pstoehr
macrumors member
 
Join Date: Aug 2003
Location: Sche▀litz, Oberfranken, Bavaria, Germany
Hi,
Quote:
Originally Posted by OldGit View Post
cocoa, objective c.

I sometimes forget there is any other method
do you want a GUI application or should the output go to the console?

If a Foundation tool is okay for you here is some code for you:
Code:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	int i;
	
	NSMutableArray *array = [[NSMutableArray alloc]init];
	for (i=0;i<10;++i)
		[array addObject:[NSString stringWithFormat:@"%d", (i*3)]];
	
	for (id obj in array)
		NSLog(@"%@",obj);
	
	[array release];
	
    [pool drain];
    return 0;
}
Best regards
Peter
pstoehr is offline   0 Reply With Quote
Old Feb 2, 2009, 05:26 PM   #6
autorelease
macrumors regular
 
Join Date: Oct 2008
Location: Achewood, CA
Quote:
Originally Posted by kainjow View Post
Code:
// one way
NSLog(@"array: %@", array);
This works for NSArrays. (as well as NSDictionaries, NSSets, etc.) If you have a plain C array, you'll have to write a loop to print each element. (This also assumes you know the size of the array.)

The %@ format specifier, more specifically, uses the output of the object's "description" method. Most Foundation and AppKit classes implement this method, so you can use it to print all kinds of objects and get meaningful output. (For example, -[NSArray description] returns an NSString composed of the descriptions of all its elements.)
autorelease is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Console - Lots of errors? Help a newbie 8Apples OS X Mavericks (10.9) 6 Dec 14, 2013 04:29 PM
Erasing and syncing replaces the contents of this iPhone with the contents of this... patent10021 iOS 6 0 Apr 8, 2013 03:19 AM
Print queue loses items to print + auto-generate PDF when printing Woutje Mac Basics and Help 0 Nov 29, 2012 10:09 AM
Print Button (Print Documents, Photos, Web Pages from your iPhone or iPad) nexyer iPhone and iPod touch Apps 9 Nov 28, 2012 06:24 AM
How can I print to file (simple output) from the print dialog? Not PDF or PostScript hachiroku OS X 0 Oct 24, 2012 04:48 PM

Forum Jump

All times are GMT -5. The time now is 04:08 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC