Help with my header file in cocoa

Discussion in 'Mac Programming' started by italiano40, Jul 22, 2008.

  1. italiano40 macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY
    #1
    this is my header file
    Code:
    #import <Cocoa/Cocoa.h>
    #import <Foundation/NSTask.h>
    
    @interface upload: NSObject{
        IBOutlet id afteruploadlabel;
        IBOutlet id iagreebutton;
        IBOutlet id ipaddresstextbox;
        IBOutlet id progressbar;
        IBOutlet id showimagelabel;
        IBOutlet id uploadbutton;
        NSString *fileName; 
    }
    - (IBAction)agree:(id)sender;
    - (IBAction)upload:(id)sender;
    @end
    now in my .m file i use fileName in my two methods
    but i get this error
    what should i do?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Mind posting your implementation file?

    Did you import your .h file?

    -Lee
     
  3. italiano40 thread starter macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY
    #3
    yea i did import my .h file

    here is the code that is flaging the error
    i didn't include the second method
    Code:
    #import "upload.h"
    #import "Foundation/NSTask.h"
    @implementation NSObject
    
    - (IBAction)agree:(id)sender { 
    	int i; // Loop counter.
    	
    	// Create the File Open Dialog class.
    	NSOpenPanel* openDlg = [NSOpenPanel openPanel];
    	
    	// Enable the selection of files in the dialog.
    	[openDlg setCanChooseFiles:YES];
    	
    	// Enable the selection of directories in the dialog.
    	[openDlg setCanChooseDirectories:YES];
    	
    	// Display the dialog.  If the OK button was pressed,
    	// process the files.
    	if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
    	{
    		// Get an array containing the full filenames of all
    		// files and directories selected.
    		NSArray* files = [openDlg filenames];
    		
    		// Loop through all the files and process them.
    		for( i = 0; i < [files count]; i++ )
    		{
    			NSString* fileName = [files objectAtIndex:i];
    			[uploadbutton hidden:false];
    			[showimagelabel setStringValue: fileName];
    		}
    	}
    	
    }
    //the second method is after this
    
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    This looks bad... you have:
    Code:
    NSString* fileName = [files objectAtIndex:i];
    Masking your class member called fileName. Your new definition's scope is the for loop that it's in. I don't see anything that would point to where the error comes from, though.

    Is the error in the function you posted or the second function? What line number is the error associated with?

    Also, and this is just a point of style, I'd always put the * in a pointer declaration next to the variable and not the type. Syntactically either will work, but if you use a single line for multiple declarations it can be confusing, i.e.:
    Code:
    int* x,y;
    
    x is an int pointer, y is an int. It might be obvious to some, but with int* there... it can be misleading.

    -Lee
     
  5. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #5
    Rename the variable fileName in the for loop.
     
  6. italiano40 thread starter macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY
    #6
    Code:
    #import "upload.h"
    #import "Foundation/NSTask.h"
    @implementation NSObject
    
    - (IBAction)agree:(id)sender { 
    	int i; // Loop counter.
    	
    	// Create the File Open Dialog class.
    	NSOpenPanel* openDlg = [NSOpenPanel openPanel];
    	
    	// Enable the selection of files in the dialog.
    	[openDlg setCanChooseFiles:YES];
    	
    	// Enable the selection of directories in the dialog.
    	[openDlg setCanChooseDirectories:YES];
    	
    	// Display the dialog.  If the OK button was pressed,
    	// process the files.
    	if ( [openDlg runModalForDirectory:nil file:nil] == NSOKButton )
    	{
    		// Get an array containing the full filenames of all
    		// files and directories selected.
    		NSArray* files = [openDlg filenames];
    		
    		// Loop through all the files and process them.
    		for( i = 0; i < [files count]; i++ )
    		{
    			NSString* fileName = [files objectAtIndex:i];//the error is here
    			[uploadbutton hidden:false];
    			[showimagelabel setStringValue: fileName];
    		}
    	}
    	
    }
    //the second method is after this
    i am just confused why my header file isn't working correct and it gives me an error that it hasn't been declare, when it has?
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    balls, it was something simple after all. Change your @implementation line from NSObject to upload.

    -Lee
     
  8. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    You are trying to implement a method for class "NSObject". Class NSObject doesn't have a member named "filename", class "upload" does.

    Also, it is very very bad style and really asking for trouble using the same name "upload" both for a class and for a member of that class. You are just going to confuse yourself.
     
  9. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #9
    If you specify to create a class in xCode it will take care of having the right class names in the files for you.
     
  10. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #10
    Also as gnasher729 said, you should consider your class names/properties more carefully. Ibelieve that Apple's dev site has a document about naming conventions.
     
  11. italiano40 thread starter macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY
  12. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #12
    It is especially important with class members, properties and getter/setter methods: Have a look at

    http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/

    There is a lot of functionality in Cocoa that you only get if you choose your names properly.
     
  13. italiano40 thread starter macrumors 65816

    italiano40

    Joined:
    Oct 7, 2007
    Location:
    NY
    #13
    i know i have very bad var. naming habits that learned from my friend
     

Share This Page