objective c program

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

  1. jamesapp macrumors 6502a

    Joined:
    Mar 7, 2008
    #1
    working on a program from the Kochan book

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

    Code:
    // 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
    Code:
    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
    Code:
    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: (
        2479333963,
        2427334907,
        2479363338,
        2479356492,
        2479356690
    )
    Trace/BPT trap
    
    any help would be appreciated
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    + 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.

    Code:
    -fileExistAtPath:isDirectory:
    should be
    Code:
    -fileExistsAtPath:isDirectory:
    Code:
    -removeFilePath:handler
    should be
    Code:
    -removeFileAtPath:handler
    Not sure about anything else.

    -Lee
     
  3. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
  4. anonymous3025 macrumors newbie

    Joined:
    May 2, 2008
    #4
    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;
    @end


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

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


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

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    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

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    I don't see a main function, so I'm guessing some things were left out. There is probably a main function that calls:
    Code:
    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.

    -Lee
     
  7. jeremyn9 macrumors newbie

    Joined:
    Jan 5, 2009
    #7
    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
    jere
     
  8. Catfish_Man macrumors 68030

    Catfish_Man

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

    Joined:
    Jan 5, 2009

Share This Page