I have been doing some reading and a little bit confused, let me explain.
My situation:
ClassA.h has some arrays, and ClassA.m has some methods that uses these arrays. I also have ClassB, ClassC, ClassD… that have their own arrays, and their own methods that use these arrays. Up until now I have just been copying and pasting the code into each class.
What I want to do:
I did some reading and found that I want to create a parent class, where all the methods are housed, and then use these methods in my classes:
//parentClass.m
@implementation parentClass
+ (void)commonMethod:(id)sender{
...
}
@end
//classA.m
@implementation classA
- (void)someMethod{
[parentsClass commonMethod];
}
@end
//classB.m
@implementation classB
- (void)someMethod{
[parentClass commonMethod];
}
@end
The Problem:
So I moved the methods to the parentClass.m, and the arrays to the parentClass.h. My problem is when I try to build and run, I get errors like Instance variable <someArray> accessed in class method. I am not sure what to do. Is the only way around this problem to declare my arrays above the @interface in the .h file?
EDIT: The reason I want to use this technique for using methods in the parent class is because I would like to call the methods like this:
[commonClass commonMethod];
And not have to declare an instance of the parent class every time.
THANKS!
Regardless of inheritance, you have to use instance methods (and not class methods) if you want to access instance variables, like your array. An instance method is indicated by a
-(minus) before the method signature, while class methods use a+.