Category Not Recognizing Ivars
-
03-12-2019 - |
Pregunta
I have quite a large View Controller in my app and I want to clean it up by separating some of the functions into categories. I read up on how to implement a category and have:
#import "StatsVC.h"
@interface StatsVC (TableViewDelegate)
@end
As my .h (the category is called StatsVC+TableViewDelegate). and my Implemation starts like this:
@implementation StatsVC (TableViewDelegate)
Several times in the category, I access variables which are iVars of the original class.I have read and supposedly this is allowed, but for every use of an iVar in the category I get this error message:
'Use of undeclared identifier 'iVar'
Does anybody know why this is happening?
Thanks,
Solución
You definitely can access instance variables from methods in a category. Categories add methods to the class -- there's no real difference at run time between a method declared in the class interface and one declared in a category on that class. You'll probably need to show more code to get a good answer. Right now, my top two guesses are:
You're trying to access ivars from a class method rather than an instance method, i.e. one starting with a
+
instead of a-
.There's something wrong with the way you've declared the ivars.