Billy Pilgrim
Billy Pilgrim

Reputation: 1852

Objective-C does ARC require inheriting NSObject?

I have written Objective-C before (a year or so ago), but it was before ARC. I have a class that has no need to inherit from NSObject (or any other NS* class), but if it doesn't, I get this error when attempting to instantiate a singleton:

+(Operator *) getInstance
{
  static Operator * g_instance = NULL;

  if (NULL == g_instance)
  {
      @synchronized( self )
      {
         g_instance = [[Operator alloc] init];
      }
  }

  return( g_instance );
}

no known class for selector 'alloc' which is listed as an ARC issue.
Does ARC now require that all classes inherit from NSObject ? Or am I missing a bigger idea?

Upvotes: 1

Views: 454

Answers (3)

mmmmmm
mmmmmm

Reputation: 32710

If you are using Objective C each class must inherit from a base class which provides message handling and lifetime management.

Originally there was an Object class but from NexTStep 3 the Apple and Gnu compiler and runtime changed to use NSObject. There are also another base class NSProxy mainly used for distributed objects. NSObject also provides basic Cocoa functionality like KVO. see Apple's "The Root class" and this SO question.

In your case the compiler cant find a definition of the alloc message but at runtime it would not know how to send any message to a Operator class if one was created.

Upvotes: 0

rdelmar
rdelmar

Reputation: 104092

I don't think this has anything to do with ARC. Alloc is a method of NSObject, so if you don't inherit form NSObject how do you expect to use Alloc?

Upvotes: 0

paulbailey
paulbailey

Reputation: 5346

Well, if you're calling alloc on your class, not providing an implementation, and not inheriting NSObject's, I'm not sure what you expect it to do?

Upvotes: 3

Related Questions