1

I'm starting Objective-C Development and I'm trying to wrap my head around how some things work.

I'm creating a Twitter Client and have a class "Tweet". This is just a DTO - a class that has some variables.

Should this just be a class or should it inherit from NSObject?

Why? Or why not?

Michael Stum
  • 177,530
  • 117
  • 400
  • 535
  • Same question http://stackoverflow.com/questions/1588281/why-subclass-nsobject –  Jul 25 '10 at 06:20

2 Answers2

4

Every normal class should subclass from NSObject. Basic memory management like -retain and -release, and runtime introspection like -isKindOfClass:, -respondsToSelector: cannot work without it.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 1
    Absolutely, every class should have either NSObject or NSProxy as its root object unless you really want to do tons of extra work and suffer a great deal. – theMikeSwan Jul 25 '10 at 06:58
1

You should, unless you have a very explicit, strong reason not to. In Objective-C, any object can be assigned to an id. But id doesn't guarantee any methods at all, even the standard operations such as allocation, initialization, deallocation and reference counting.

These are implemented by a root class, which is almost always an NSObject.

Also, most of the Cocoa API expects to deal with an NSObject, because it needs to at least retain and release the object.

So you should inherit from NSObject.

Yuji
  • 34,103
  • 3
  • 70
  • 88