Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 2, 2009, 12: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, 01: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, 02: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, 03: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, 04: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, 04: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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
weird clitch when printing list of apps to pdf mrgreeneyes Mac Applications and Mac App Store 0 Aug 23, 2010 01:27 PM
Put the entire contents of mysql_fetch_array into another array Malfoy Web Design and Development 4 May 23, 2008 11:39 AM
Transferring contents of ipod to new mac's itunes sailortena iPod 2 Jan 25, 2008 11:41 AM
How to print contents of a link ? TheBrazilianGuy Mac Applications and Mac App Store 2 Aug 3, 2006 01:20 PM
Copy contents of DVD to hard-drive? Versello Mac Applications and Mac App Store 16 May 8, 2004 10:38 AM


All times are GMT -5. The time now is 01:46 PM.

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

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