I've got some code where my classes inherit from a superclass, and everything has been working fine till now. I'm getting an error whenever I try to use any of the superclass variables, saying that they are undeclared (first use in this function). It's only happening in one of my subclasses, & it looks exactly the same as the others. I'm wondering if there's anything obvious which I should know about (being quite new to Objective-C). The basic code is like -
@interface mySuperClass : UIViewController {
BOOL myVar;
}
Then -
@interface mySubClass : mySuperClass {
}
@implementation mySubClass {
-(void)someMethod {
myVar = YES; // error here
}
@end
Any help much appreciated - if you need more info, let me know! Thanks.
Using public instance variables is considered to be bad form in object oriented programming because they are dangerous. If anyone in the world can change the value of an instance variable, debugging is difficult. It is better to restrict access to instance variables by making them private.
Instance variables are defined as outside from any method declaration. They are used to represent the state of the objects, and every object of the class has its copy of the instance members.
Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are given accessibility), they should be called using the fully qualified name.
An instance variable is a variable that exists and holds its value for the life of the object. The memory used for instance variables is allocated when the object is first created (through alloc), and freed when the object is deallocated.
I just got over a very similar strange error where I could no longer access properties in my superclass, and xcode was givine me compiler errors saying "(*) undeclared (first use in this function)". However I had not any problems in the past...
The problem was that I had introduced typos at the top of my .m file and the xcode compiler output was misleading me. Specifically I had @synthesize statements where the properties were misspelled, either in the synthesize statement or in the corresponding variable in the headerfile.
If you have @synthesize statements or other declarations, examine them with a fine toothed comb (i.e. which lines have you introduce most recently?), or even comment out a block of them to see if you can compile again and narrow down the culprit.
Again the compiler errors were very misleading, so it really was tough to debug. Although like 99.9% of the time the error was my own. :)
I can't see anything wrong with the code you've pasted. My first guess would be that you're not importing the mySuperClass .h file properly. Ie you're missing
#import "mySuperClass.h" //or #include
In mySubClass.h, or mySubClass.m
I ran into the same problem and after wasting hours I finally solved it in my project:
I refactored my source and removed a member but forgot to remove the @property definition in .h file. This leads to errors at every place when I am using super members. BTW: Methods from super are fine.
So check your property defs. Thanks to Screwtape in post 8 for his solution :-)
EDIT: Oops, Kris' answer is pretty similar except from typos vs. removal.
I am having the same issue for weeks
"Variable Undeclared" error when compiling to iOS Device, but not for Simulator
One solution I found is to simply change the compiler from the default LLVM GCC 4.2
to LLVM Compiler 2.0
(or to `Apple LLVM Compiler 2.1). It seems to be a bug in the compiler, but it is just a guess.
Changing it is a quick fix for your problem if there is no need for you to use the GCC compiler at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With