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

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.

Related

Crash happening when using removeObjectsInArray:[NSArray arrayWithArray:] [duplicate]

This question already has answers here:
crash while removing objects from NSMutableArray
(6 answers)
Closed 8 years ago.
self.filteredProducts = [[NSMutableArray alloc] init];
//Find products
self.filteredProducts = (NSMutableArray*)[(NSMutableDictionary *)[self.CECategoriesDictionary objectForKey:category.categoryGuid] allValues];
if([self.oldfilteredProducts count] > 0 && selectedLevel==5)
{
NSMutableArray *diff = [self.filteredProducts mutableCopy];
[diff removeObjectsInArray:[NSArray arrayWithArray:self.oldfilteredProducts]];
[self.filteredProducts removeObjectsInArray:[NSArray arrayWithArray:diff]];
}
Logs :
[__NSArrayI removeObjectsInArray:]: unrecognized selector sent to instance 0x1aed19d0
2014-09-15 22:00:19.243 SAPRetailEx[10325:90b] [ERROR]:Uncaught exception: -[__NSArrayI removeObjectsInArray:]: unrecognized selector sent to instance 0x1aed19d0
2014-09-15 22:00:19.243 SAPRetailEx[10325:90b] [ERROR]:-[__NSArrayI removeObjectsInArray:]: unrecognized selector sent to instance 0x1aed19d0
2014-09-15 22:00:19.248 SAPRetailEx[10325:90b] [ERROR]:(
you cannot remove objects form NSArray; I know you think you are using an NSMutableArray but you are not. This is made quite clear from your error [__NSArrayI removeObjectsInArray:]: unrecognized selector; __NSArrayI is an immutable object (that is what the I means), if this were really a mutable array the class would be __NSArrayM.
you seem to be operating under the false impression that you can get a mutable array simply by casting an NSArray to NSMutableArray; this has never been how casting works under any C derived language. You instead need to send a mutableCopy message to the NSArray. Of course you now have a copy of the array so modifications arent reflected in the original. you also own the copy so if you arent using ARC you need to release the copy when you are done.
I'm curious to know why you are already making a mutableCopy of self.filteredProducts then also trying to remove directly from self.filteredProducts. even if this worked how you expect you are now copying the array for no reason then removing from both thee "original" and the "copy".
self.filteredProducts isn't really an NSMutableArray It's just a normal NSArray which doesn't have a "remove option".
[self.filteredProducts removeObjectsInArray:[NSArray arrayWithArray:diff]
To help in the future... set at break point in your code after you assign it, then print out the description and see what type of object it really is. either po in the debugger or right click on "Print Description". Don't believe what is says on the left panel, that's just what you told the code the object is, look at the description on the right to see what it really is.

Initialization of an NSMutableArray with array or alloc init [duplicate]

This question already has answers here:
Difference between [NSMutableArray array] vs [[NSMutableArray alloc] init]
(2 answers)
Closed 9 years ago.
I am new to Obejtive C so im looking at alot of sample code at the time and i noticed that people initialize their NSMutableArray differently.
NSMutableArray *items = [NSMutableArray array];
or
NSMutableArray *items = [[NSMutableArray alloc] init];
In both lines you end up with an NSMutableArray Object.
What is the difference between them or are they exactly the same?
The main difference between these is if you're not using ARC (Automatic Reference Counting). The first one returns a retained and autoreleased object. The second one returns an object that is only retained. So in the first case, you would want to retain it if you wanted to keep it around for longer than the current run loop. In the second case, you would want to release or autorelease it if you didn't want to keep it around.
Now that we have ARC, this changes things. Basically, in ARC code, it doesn't matter which of these you use.

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”.

NSString retain count in Objective-C [duplicate]

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.

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.