Why does the compiler say I haven't declared my variable?
I am getting an error in Xcode that says "whatEverVariable is undeclared" This is happening inside the .m file and also outside of the class itself. I have declared the variable inside the curly braces of the .h file and am using @property and @synthesize to access the variable. However I am getting that above error in the .m file sometimes and even in other classes where I have #imported the class that contains the variable. Am I missing something about Objective-C in general here or what? I guess what I am asking is why would things be undeclared if they are declared.
Also I am getting "button may not respond to "setTitle" as an error as well has this been deprecated or something to "setTitle: forState:" because I am not wanting to use that.
I would post an example but I really think I am missing some obvious thing here. Does anyone have any thoughts on what would cause the errors? Sorry to be so general, but I have to be a moron or something?
This is saying "picturesInArray is undeclared"
#import <Foundation/Foundation.h>
@interface Pictures : NSObject {
NSMutableArray *picturesInArray;
}
@property (nonatomic, retain) NSMutableArray *picturesInArray;
-(IBAction)pictureButtonPressed:(id)sender;
@end
#import "Pictures.h"
@implementation Pictures
@synthesize picturesInArray;
-(IBAction)pictureButtonPressed:(id)sender {
if (pictureIndex >= [picturesInArray count] || pictureIndex < 0) {
pictureIndex = 0;
[imageView setImage:[picturesInArray objectAtIndex:pictureIndex]];
[imageView setNeedsDispla开发者_运维知识库y];
}
}
This is saying "
picturesInArray
is undeclared"
Are you sure it's not saying “pictureIndex
is undeclared”? Because that's the variable that's undeclared in the code you showed.
Could you post the sample code?
My guess is that you aren't declaring the object/variable type that whatEverVariable is at some point. Make sure that your header looks like this:
MyClass.h:
@interface MyClass : NSObject
{
NSString* whatEverVariable;
}
@property(readwrite, assign) NSString* whatEverVariable;
@end
MyClass.m:
@implementation MyClass
//whatever methods
@synthesize whatEverVariable;
@end
Notice that in your @property you are declaring the type of the variable again.
As for the button, make sure that it is of type NSButton.
Also, have you imported ? That imports a lot of the headers for things like NSButton.
This is saying "picturesInArray is undeclared" Sorry about putting it in "comments above it made it hard to read!
#import <Foundation/Foundation.h>
@interface Pictures : NSObject {
NSMutableArray *picturesInArray;
}
@property (nonatomic, retain) NSMutableArray *picturesInArray;
-(IBAction)pictureButtonPressed:(id)sender;
@end
#import "Pictures.h"
@implementation Pictures
@synthesize picturesInArray;
-(IBAction)pictureButtonPressed:(id)sender {
if (pictureIndex >= [picturesInArray count] || pictureIndex < 0) {
pictureIndex = 0;
[imageView setImage:[picturesInArray objectAtIndex:pictureIndex]];
[imageView setNeedsDisplay];
}
}
精彩评论