Subclassing NSObject, can it cause problems?
I have a very basic data class that is subclassed from NSObject. I declare a few strings, make sure they have properties (nonatomic, copy), and synthesize them. The only method I implemented was dealloc() which releases my strings. Can any memory problems arise from just开发者_如何学编程 this? Are there any other methods I need to implement?
Subclassing NSObject is something that we do all the time. Just follow the memory management rules, and you're good to go.
You could implement a custom init if you want to set anything up.
-(id)init {
if (!(self = [super init]))
return nil;
// Set things up you might need setting up.
return self;
}
But that's only if there's something you want to have ready before you call anything else on the class.
Just having a dealloc method should be fine, otherwise.
There won't be any problems. Subclassing NSObject
is perfectly accepted, and in 99% of cases required.
By subclassing NSObject
, your subclass receives all the required behaviour that is expected of any object in Cocoa/Cocoa Touch. This includes things like the reference counting memory management system using retain
and release
etc.
What you're doing is fine. Be sure to call [super dealloc]
at the end of your subclass' -dealloc
method.
精彩评论