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.