Resolved A command line app

Discussion in 'Mac Programming' started by multinode, Jun 2, 2011.

  1. multinode, Jun 2, 2011
    Last edited: Jun 3, 2011

    multinode macrumors regular

    Feb 4, 2011
    I have a need to generate a command line app that will prompt the user to input 10 entries ... one at a time. The app must take each entry, convert it into an NSString, and add that NSString to an NSArray. After those 10 entries have been entered, the NSArray must be added to an existing NSDictionary.

    I've never done a command line app in XCode, so I'm asking for some starting point help:

    1. Command line debugging
    2. Classes to use to service the keyboard entries.

    Any pointers to relevant docs would be appreciated.
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    man gdb

    If you're familiar with scanf() and friends, you can use them. Or anything else in <stdio.h>. Or in any other C library.

    You can tell Xcode to open the man page for any command, function, etc. Open man page is under Xcode's Help menu.

    See the basic Xcode project mgmt docs.

    In Xcode Application templates (Xcode 3.*),
    choose the Command Line Tool template,
    from the Type popup
    choose Foundation.

    Example of a simple command-line Foundation tool:
    #import <Foundation/Foundation.h>
    int main( int argc, const char * argv[] )
    	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    	NSString * cwd = nil;
    		char cbuf[ 2000 ];
    		getcwd( cbuf, sizeof(cbuf) );
    		cwd = [NSString stringWithUTF8String:cbuf];
    	NSLog( @"cwd: %@", cwd );
    	NSLog( @"More stuff goes here." );
    	[pool drain];
    	return 0;
    The C function getcwd() gets the working directory pathname in cbuf. The stringWithUTF8String: method converts it to an NSString. The returned NSString is NOT owned by the example code shown.

    You will need to understand ownership if you use retain/release. You may find garbage collection worthwhile, as it can save some effort in managing ownership.

    You will also want to read the entire NSString class reference doc. Really. It has a lot of methods useful for converting to/from C strings.
  3. multinode thread starter macrumors regular

    Feb 4, 2011
    Thanx CHOWN33. Back in the "olden days" if the program included getchar() a command line terminal would pop up. But I see now that the XCode console serves that purpose.

    I misstated what I was looking for vis-a-vis debugging. I shouldn't have said that I was looking for command line debugging ... I was really looking to debug the command line app using XCode's debugger. And I see that that that works fine.

    Your instructions regarding a new project helped.


Share This Page