1.
NSMutableArray *content =[NSMutableArray new];
2.
NSMutableArray *content = [[NSMutableArray alloc] init];
in this what is the retain cout for(1), and (2).
1.
NSMutableArray *content =[NSMutableArray new];
2.
NSMutableArray *content = [[NSMutableArray alloc] init];
in this what is the retain cout for(1), and (2).
Don't use retainCount
[1]
You should never use -retainCount, because it never tells you anything useful. The implementation of the Foundation and AppKit/UIKit frameworks is opaque; you don't know what's being retained, why it's being retained, who's retaining it, when it was retained, and so on.-Dave DeLong
NSMutableArray *content =[NSMutableArray new]; //new will increase retainCount of content by 1
NSMutableArray *content = [[NSMutableArray alloc] init];//alloc will increase retainCount of content by 1
Alloc : Class method of NSObject. Returns a new instance of the receiving class.
Init : Instance method of NSObject. Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated.
New : Class method of NSObject. Allocates a new instance of the receiving class, sends it an init message, and returns the initialized object.
retain count is 1 for both
The two expressions are identical in result. +new
is just short hand for calling alloc
and init
. Literally, new
is implemented as:
+ new { return [[self alloc] init]; }
Thus, both return an object with a retain count of +1. Not 1, but +1. That is, they return an object that your code must call release
upon when your code is done with the object.
The absolute retain count is meaningless. Internally, NSMutableArray
might retain
the object 74 times and as long as it releases it 74 times as needed, that would be valid. Of course, NSMutableArray
doesn't really play such shenanigans, but some classes do (especially the more complex classes that play games with the network and/or the UI).
They are both 1. However you shouldn't be concerned about that. Remember:
If you allocate an object you are responsible for releasing it (I assume you are using MRR).
For (1) and (2), the retain count is 1, and unless you are using ARC be sure of releasing it somewhere.
In both Array Retain count is 1
retainCount of an object changes in the following cases:
1) When you create an object(new or alloc, copy or mutablecopy), it has a retain count of 1.
2) When you send an object a retain message, its retain count is incremented by 1.
3) When you send an object a release message, its retain count is decremented by 1.
When you send a autorelease message to an object, its retain count will be decremented by 1(not immediately as in case of release but some time in the future)
The +new method is simply shorthand for +alloc and -init. The ownership semantics are identical. The only benefit to using +new is that it is more concise. If you need to provide arguments to the class's initialiser, you will have to use the +alloc and -initWith... methods instead. So the retain count will be 1 for both.