1

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?

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Billy Pilgrim
  • 1,842
  • 3
  • 22
  • 32
  • If you're going to program with Cocoa, you should inherit from `NSObject`. The real question, though, is: did you implement `alloc` yourself? – jscs Jul 23 '12 at 20:57

3 Answers3

3

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?

paulbailey
  • 5,328
  • 22
  • 35
  • hmmmm... could be idiocy on my part, but an class doesn't require an implementation, does it? I can have an object that contains member variables, and synthesizes them, but doesn't have any explicit methods. (Whether it is sane is a (potentially) different point:). It can still be instantiated w/o explicit alloc method (simple alloc of appropriate size) -- yes? no? – Billy Pilgrim Jul 23 '12 at 21:05
  • 2
    @Billy: No. You could certainly create an instance via [`class_createInstance()`](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html),or possibly even `calloc(1, class_getInstanceSize(self))`, but you definitely can't send the message `alloc` to a class that doesn't implement it and expect it to not crash. – jscs Jul 23 '12 at 21:10
0

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?

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • okay -- I get it. I mistakenly believed that 'alloc' is similar (equivalent) to 'malloc'. If an object does not inherit from NSObject, how *are* they instantiated? Thanks for the whack upside the head :) – Billy Pilgrim Jul 23 '12 at 21:11
0

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.

Community
  • 1
  • 1
mmmmmm
  • 32,227
  • 27
  • 88
  • 117