I have a category on an existing class that adds a property and a few methods to the class.
@interface AClass (ACategory) {
NSString *aProperty;
}
@property (nonatomic, retain) NSString *aProperty;
@end
In the implementation file, I want to release this property when the object is deallocated. However, if I declare dealloc in this class, it will override the dealloc from the original class from what I understand. What then is the proper way to release this aProperty when the object is deallocated?
@implementation AClass (ACategory)
@synthesize aProperty;
- (void)dealloc {
[aProperty release];
// this will skip the original dealloc method from what I understand
[super dealloc];
}
@end
Well, this is a little problematic, since your code is wrong.
@interface AClass () {//...), but that is different from a category (@interface AClass (ACategory)).@interfaceline.You can declare a property in a category, but you’ll have to define its storage without using a new instance variable (hence,
@dynamicinstead of@synthesize).As to your actual question, you can’t call the original implementation of an overridden method unless you use method-swizzling (facilitated by runtime functions like
method_exchangeImplementations). I recommend against doing this anyway; it’s really frightening and dangerous.Update: Explanation of Instance Variables in Class Extensions
A class extension is like a category, but it is anonymous and must be placed within the
.mfile associated with the original class. It looks like:Its implementation must be in the main
@implementationblock for your class. Thus:So, a class extension is useful for keeping private instance variables and methods out of the public interface, but will not help you add instance variables to a class over which you haven’t control. There is no issue with overriding
-dealloc, because you just implement it like you normally would, whilst including any necessary memory management for the instance variables you introduced within the class extension.Please note that this stuff works only with the latest 64-bit Objective-C ABI.