objective c program

Discussion in 'Mac Programming' started by jamesapp, May 1, 2008.

  1. jamesapp macrumors 6502a

    Mar 7, 2008
    working on a program from the Kochan book

    here is my test file which i called prog16.6.m

    // implement a basic copy utiltity
    #import <Foundation/NSString.h>
    #import <Foundation/NSArray.h>
    #import <Foundation/NSFileManager.h>
    #import <Foundation/NSAutoreleasePool.h>
    #import <Foundation/NSPathUtilities.h>
    #import <Foundation/NSProcessInfo.h>
    #import <stdio.h>
    int main (int argc, char *argv[])
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      NSFileManager *NSFm;
      NSString *source, *dest;
      BOOL isDir;
      NSProcessInfo *proc = [NSProcessInfo processInfo];
      NSArray *args = [NSProcessInfo arguments];
      NSFm = [NSFileManager defaultManager];
      // check for two arguments on the command line
      if ([args count] != 3) {
          printf ("Usage: %s src dest\n", [[proc processName] UTF8String]);
          return 1;
          source = [args objectAtIndex: 1];
          dest = [args objectAtIndex: 2];
      // make sure the source file cna be read
      if ([NSFm isReadableFileAtPath: source] == NO) {
             printf ("Can't read %s\n", [source UTF8String]);
             return 2;
       // see if the destination file is a directory
       // if it is, add the source to the end of the destination
       [NSFm fileExistAtPath: dest isDirectory: &isDir];
       if (isDir == YES)
             dest = [dest stringByAppendingPathComponent:
             [source lastPathComponent]];
       // remove the destination file if it already exists
       [NSFm removeFilePath: dest handler: nil];
       // okay, time to perform the copy
       if ([NSFm copyPath: source toPath: dest handler: nil] == NO) {
             printf ("Copy failed!\n");
             return 4;
       printf ("Copy of %s to %s succeeded!\n", [source UTF8String],
             [dest UTF8String]);
      [pool release];
      return 0;
    the program compiled with some warning messages. here are the warning messages i am getting when i compiled the program in terminal
    james-collinss-macbook-pro:prog16 jamescollins$ gcc -framework Foundation prog16.6.m -o prog16.6
    prog16.6.m: In function ‘main’:
    prog16.6.m:18: warning: ‘NSProcessInfo’ may not respond to ‘+arguments’
    prog16.6.m:18: warning: (Messages without a matching method signature
    prog16.6.m:18: warning: will be assumed to return ‘id’ and accept
    prog16.6.m:18: warning: ‘...’ as arguments.)
    prog16.6.m:42: warning: ‘NSFileManager’ may not respond to ‘-fileExistAtPath:isDirectory:’
    prog16.6.m:50: warning: ‘NSFileManager’ may not respond to ‘-removeFilePath:handler:’
    and here is the output i get when i try to run the program from terminal
    james-collinss-macbook-pro:prog16 jamescollins$ ./prog16.6
    2008-05-01 10:50:27.925 prog16.6[273:10b] *** +[NSProcessInfo arguments]: unrecognized selector sent to class 0xa0381f00
    2008-05-01 10:50:27.928 prog16.6[273:10b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSProcessInfo arguments]: unrecognized selector sent to class 0xa0381f00'
    2008-05-01 10:50:27.928 prog16.6[273:10b] Stack: (
    Trace/BPT trap
    any help would be appreciated
  2. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    + methods are static elements. They are invoked on the class.
    - methods are called on instances of the class.

    You are calling arguments on NSProcessInfo instead of proc.

    should be
    should be
    Not sure about anything else.

  3. jamesapp thread starter macrumors 6502a

    Mar 7, 2008
  4. anonymous3025 macrumors newbie

    May 2, 2008
    Please help me with this objective-c/cocoa

    Hello, I am new to objective-C and Cocoa. I tried to make an app where you enter text and there is an if statement that decides what happens. Except even if a enter the correct info the else statement still executes. Here is the code:

    //This is the header file
    #import <Cocoa/Cocoa.h>

    @interface Who : NSObject {
    IBOutlet id input;
    IBOutlet id text;
    - (IBAction)who:(id)sender;

    //This is the implementation file
    #import "Who.h"

    @implementation Who
    - (IBAction)who:(id)sender {
    if (input==@"Tim")
    [text setStringValue:mad:"Hi tim"];
    [text setStringValue:mad:"I do not know you"];

    Someone please help me!!!
  5. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    I don't see any code there that reads the text you put in a text box or any code that reads from the standard input. As far as I can tell input is just an uninitialised variable at the moment with no useful contents.

    Saying that I don't have much experience with Objective-C.
  6. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    I don't see a main function, so I'm guessing some things were left out. There is probably a main function that calls:
    return NSApplicationMain(argc,  (const char **) argv);
    And there is probably a nib or two associated with his class, that have some fields tied to the IBOutlets and a button tied to the IBAction.

    Without the other information it is hard to tell what's going on. I am not at my mac so I can't try it out. I should probably have said start a new thread, as this one has now been completely hijacked, but oh well.

    To anonymous3025, please start a new thread and post the information about your NIB and the connections you have made to the Who class.

  7. jeremyn9 macrumors newbie

    Jan 5, 2009
    Problem with finding Foundation class

    Hi Im also trying a code similar to urs from Kochan's book. However, im using cygwin and do not have the foundation classes. Im having trouble finding the classes online, can any1 direct me to where i can download the foundation classes??

    Any help is greatly appreciated

    Obj C Newbie
  8. Catfish_Man macrumors 68030


    Sep 13, 2001
    Portland, OR
    You'll probably need to use GNUStep since Apple's Foundation implementation is not open source or Windows compatible.
  9. jeremyn9 macrumors newbie

    Jan 5, 2009

Share This Page