NSString retain count in Objective-C [duplicate] - objective-c

This question already has answers here:
When to use -retainCount?
(11 answers)
Closed 9 years ago.
NSString* nsString=[[NSString alloc]initWithString:#"nsString"];
NSLog(#"nsString RetainCount:%li",[nsString retainCount]);
the corresponding result is:
2013-03-04 11:18:03.291 ARC[655:303] nsString RetainCount:-1
in addition:
if use init a NSMutableString instance;
it return 1;

http://whentouseretaincount.com
Immutable NSStrings generated at compile time are singletons. Thus, they don't do the retain/release dance at all.
NSString detects when it is initialized with such and just returns the string directly. You'd find that the object returned by alloc in that code is different than the one returned by the init... call.

Related

How to modify value in NSArray object? [duplicate]

This question already has answers here:
How do you change the elements within an NSArray?
(3 answers)
Closed 7 years ago.
I want to modify value in NSArray at index 0.
Here is sample program
NSArray *test = #[#"abc",#"rec",#"myPM"];
NSLog (#"%#",[test objectAtIndex:0]);
test[0]=#"001";
NSLog (#"%#",[test objectAtIndex:0]);
Here I am getting lot of errors. please help me in this.
You should use an NSMutableArray
And with this method :
[yourArray replaceObjectAtIndex:INDEX withObject:OBJECT].

What is the point of nil when init NSArray? [duplicate]

This question already has answers here:
Why does NSArray arrayWithObjects require a terminating nil?
(6 answers)
Closed 7 years ago.
I'm new to objective-C. I'm just read some of article, and I saw that when init Array or Set, the writer do something like:
NSArray *list=[[NSArray alloc] initWithObjects:#"Andy",#"Erik",#"Aaron",nil];
I'm try to not use nil and nothing change, so, what is the point of nil when init an array or a set?
That's how the 'method' 'knows' that you've given it the last item
in the list so it can know for sure when it reaches the end of the
arguments for the method.
NSArray *listdemo = [[NSArray alloc] initWithObjects:#"Andy",#"Erik",#"Aaron",nil];
The method is iterating
through the list until it finds nil. If it doesn't, it runs off the
end and Bad Things happen.

Technical term for accessing an array with an index and square brackets [duplicate]

This question already has answers here:
What does a square-bracketed index after an NSArray mean? [duplicate]
(2 answers)
Closed 9 years ago.
What is the term or name of the operation for getting member of an array? For example, this method returns a simple array:
- (NSArray*)createArray
{
NSArray *myArray = [[NSArray alloc] initWithObjects:#"unordentliches array", #"beliebiges Value", nil];
return myArray;
}
And I can NSLog one of its elements in the following way:
NSLog(#"%#", [self createArray][1]);
Output:
beliebiges Value
Good, no problem here.
But what do we call this operation: [self createArray][1]? One that allows us to -- without first assigning the value to a NSString -- simply put this [1] right next to the the returned value from a method call and output the value?
[self createArray][1];
What is the technical term for this?
Putting the element index in parentheses (or brackets in this case) after an array is called “subscripting”. The index is called a “subscript”.
There is no special name for directly subscripting the array returned by a message without storing the array in a variable first.
Under the covers, the compiler turns the subscripting into another message, like this:
[[self createArray] objectAtIndexedSubscript:1];
Sending a message directly to the object returned by another message is called “message chaining” or “method chaining”.

Memory management for classes like NSNumber, NSSet [duplicate]

This question already has answers here:
Releasing instances of NSNumber created with initWithInteger: vs. numberWithInt:
(3 answers)
Closed 9 years ago.
These are classes and they declares pointers... to objects right? You send methods to them like objects.
NSNumber * myNumber = [NSNumber numberWithInteger: x];
So why are they not released like so:
[myNumber release];
Thanks!
By convention, class method initializers like the one you've shown create autoreleased instances, so you don't need to call release unless you retain them somehow. However, the instance method initializers - like initWithInteger: in your case - return retained instances. Those you would need to release yourself.
There is a rule you should remember about memory management "NARC": When you use New Alloc Retain Copy you should release or autorelease object, otherwise you shouldn't.

If a reference is not pointing to any instance, why can the instance method still be invoked? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sending a message to nil?
If a reference to NSMutableArray is not pointing to any object at all, because none was instantiated, why can the instance method still be invoked without any run-time error?
NSMutableArray *foo = nil;
NSLog(#"[null count] is %i", [foo count]);
NSLog(#"[null count] again is %i", [(NSMutableArray *) nil count]);
The above lines both print out 0 instead of causing a bad memory access or causing an error that says there is no instance.
Direct from The Objective-C Programming Language: In Objective-C, it is valid to send a message to nil—it simply has no effect at runtime.
If you read on a little further, you'll see why [nil count] returns 0.