I’m using the static code analyser in objective C and I found that using categories to spread a big file in multiple files causes the following problem:
@interface TestClass : UIViewController
@property (nonatomic, assign) UITableView* myTableView;
@end
@implementation TestClass
@end
@interface TestClass (someCategory)
@end
@implementation TestClass (someCategory)
- (void) someMethod
{
// ...
CGRect tableViewRect =
CGRectMake( sectionRect.origin.x,
sectionRect.origin.y + sectionRect.size.height + 1.0,
sectionRect.size.width,
tableViewHeight);
myTableView = [[UITableView alloc] initWithFrame:(CGRect) tableViewRect
style:(UITableViewStyle) UITableViewStylePlain];
[self.view addSubView: (UIView*) myTableView];
[myTableView release];
}
@end
Problem # 1: Compiling TestClass(someCategory) gives me an error “use of undeclared identifier ‘theArray'”.
-> Adding the prefix “self.myTableView” seems to fix the problem.
Problem # 2: Once I have added the “self.” prefix before “myTableView”, the code analyser complains “incorrect decrement of the reference count of an object that is not owned at this point by the caller”
->I have seen this before in my code: easy to fix by removing the “self.” prefix in other, non categorized classes.
So I have a catch 22 situation!
– I can’t have a class category without prefixing the properties that I use with “self.”
– The code analyser gives me warnings because it does not seem to understand that my category owns an object that it allocates and frees.
Fixing either of these two problems would work for me
(a) finding a way to avoid specifying the “.self” prefix when referencing an attribute from my category implementation
(b) finding a way to make the code analyser happy with the fact that I own “self.xxx” where “xxx” is a property of the class that I am categorizing.
If you need to hold a reference to your table view for the life of the object, you should release it in the
deallocmethod. Saying[object release]is effectively saying you don’t need a reference to the object any more.If you don’t need a reference to the table view, there’s no need to use an instance variable/property. Just set up a temporary
UITableView *pointer in your method.Releasing an object from a property getter is usually a bad idea (see this question: Incorrect decrement of the reference count of an object that is not owned at this point by the caller)