-1

I am new to objective C. I want to know why some classes do not need alloc when building an object. For example, the class NSNumber. To build an object:

NSNumber * strangeNum;
strangeNum = [NSNumber numberWithInteger:100];

No alloc and no release are needed.

But if I define a class myself, say myClass and build an object. I need to

myClass * myObj=[[myClass alloc] init];
...
[myObj release] // if without ARC

Can somebody explain this in detail? Thanks a lot.

Peter Hornsby
  • 4,208
  • 1
  • 25
  • 44
ButterLover
  • 137
  • 1
  • 2
  • 10

3 Answers3

1

The release method has been pretty much obsoleted by ARC (automatic reference counting), and you'll rarely, if ever, come across it with newer (the past couple years) code. To understand why you don't call release on an object you get from numberWithInteger:, you need to read the memory management policy for Objective-C, which can currently be found at this link.

The alloc method is a class method, and is the canonical way to allocate memory for any class which descends from NSObject. Classes may have any number of class methods, and a class method can create an object on your behalf. Somewhere in the chain of method calls, alloc will be called, it is just that for Foundation classes, which you don't have the source for, you don't see it.

Avi
  • 7,469
  • 2
  • 21
  • 22
0

First case - +[NSNumber numberWithInteger:]. It's a class factory method. From Apple documentation:

Class factory methods should always start with the name of the class (without the prefix) that they create, with the exception of subclasses of classes with existing factory methods. In the case of the NSArray class, for example, the factory methods start with array. The NSMutableArray class doesn’t define any of its own class-specific factory methods, so the factory methods for a mutable array still begin with array.

Class factory method return autoreleased object. Usually it's used as a simple shortcut, that calls corresponding init method:

+ (NSNumber)numberWithInteger:(int)intVal {
    return [[[self alloc] initWithInteger:intVal] autorelease];
}

Second case is an explicit creation of NSNumber instance. Because you didn't call autorelease right after instance creation, you should call release after you've finished using object to free allocated memory.

So, object instance in both cases built trough alloc and init calls sequence.

Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
-2

Actually NSNumber does need an alloc, but you are using a static method which does the alloc.

If you were to see the source code for NSNumber, its static method(non instance method) for numberWithInteger would probably be something like this

-(NSNumber)numberWithInteger:(int)intVal {
   NSNumber num = [[NSNumber alloc] init];
   *Some magic*
}

Basically Apple have just made some it easier for you.

Reedy
  • 1,866
  • 2
  • 20
  • 23
  • 1
    It's a class method, not a static method. [There's no such thing as static methods in Objective-C](http://stackoverflow.com/questions/8089186/whats-the-difference-between-class-method-and-static-method/8089623#8089623). – jscs Dec 21 '15 at 19:18
  • "non instance method", its a method that doesnt require an instance(some call this static) – Reedy Dec 21 '15 at 19:19
  • Yes, it does: it requires an instance of the metaclass, which is the class object. – jscs Dec 21 '15 at 19:29
  • @Reedy But why `release` is not needed for NSNumber object when ARC is not available? Once it is `alloc`, where its corresponding `release`? – ButterLover Dec 21 '15 at 19:30
  • Im not entirely sure but it might be related to the fact that its a primitive object underneath and so it is automatically released. – Reedy Dec 21 '15 at 20:17