Sheehan Alam
Sheehan Alam

Reputation: 60899

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?

Upvotes: 3

Views: 1119

Answers (4)

Barry Wark
Barry Wark

Reputation: 107754

What you're doing is fine. Be sure to call [super dealloc] at the end of your subclass' -dealloc method.

Upvotes: 1

Jasarien
Jasarien

Reputation: 58448

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.

Upvotes: 2

Tom Irving
Tom Irving

Reputation: 10069

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.

Upvotes: 4

NSResponder
NSResponder

Reputation: 16861

Subclassing NSObject is something that we do all the time. Just follow the memory management rules, and you're good to go.

Upvotes: 9

Related Questions