View Full Version : Help with my header file in cocoa
italiano40
Jul 23, 2008, 12:11 AM
this is my header file
#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
error: 'fileName' undeclared (first use in this function)
what should i do?
lee1210
Jul 23, 2008, 01:24 AM
Mind posting your implementation file?
Did you import your .h file?
-Lee
italiano40
Jul 23, 2008, 01:28 AM
Mind posting your implementation file?
Did you import your .h file?
-Lee
yea i did import my .h file
here is the code that is flaging the error
i didn't include the second method
#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
lee1210
Jul 23, 2008, 02:08 AM
This looks bad... you have:
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.:
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
cMacSW
Jul 23, 2008, 08:22 AM
Rename the variable fileName in the for loop.
italiano40
Jul 23, 2008, 11:38 AM
#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?
lee1210
Jul 23, 2008, 12:02 PM
balls, it was something simple after all. Change your @implementation line from NSObject to upload.
-Lee
gnasher729
Jul 23, 2008, 12:02 PM
yea i did import my .h file
here is the code that is flaging the error
i didn't include the second method
#import "upload.h"
#import "Foundation/NSTask.h"
@implementation NSObject
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.
cMacSW
Jul 23, 2008, 12:07 PM
If you specify to create a class in xCode it will take care of having the right class names in the files for you.
cMacSW
Jul 23, 2008, 12:12 PM
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.
italiano40
Jul 23, 2008, 12:13 PM
thank you all, it runs great now
gnasher729
Jul 23, 2008, 01:13 PM
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.
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.
italiano40
Jul 23, 2008, 06:21 PM
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.
i know i have very bad var. naming habits that learned from my friend
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.