What is the different between NSArray Parsing? - objective-c

What is the difference between of Parsing in the following code segments.
which codes segments is faster in Parsing?
NSArray *arr = [[NSArray alloc] initWithObjects:#"Apple",#"Macbook", nil];
NSMutableArray *data = (NSMutableArray *)arr;
(and)
NSArray *arr = [[NSArray alloc] initWithObjects:#"Apple",#"Macbook", nil];
NSMutableArray *data = [NSMutableArray arrayWithArray:arr];

You have the same mistake in both snippets, you allocate memory for the object, and then assign something else to data, which makes you lose the previous (and have memory leak), e.g. this:
NSMutableArray *data = [[NSMutableArray alloc] init];
data = [NSMutableArray arrayWithArray:arr];
should be
NSMutableArray *data = [NSMutableArray arrayWithArray:arr];
Now for the question itself:
The first case is a bad idea, you cast the NSArray, but you canot modify it, as you didn't really changed its type, only assigned it to NSMutaleArray pointer.
The second case will create a new NSMutableArray which is mutable, with the contents of the NSArray, and this is cool, you may alter this array now.

It looks like what you want is either:
NSArray *arr = [[NSArray alloc] initWithObjects:#"Apple",#"Macbook", nil];
NSMutableArray *data = [arr mutableCopy];
or:
NSMutableArray *data = [[NSMutableArray alloc] initWithObjects:#"Apple",#"Macbook", nil];

Related

What are the differences between [[NSMutableArray alloc] init] and [#[] mutableCopy]?

NSMutableArray *myArray = [[NSMutableArray alloc] init];
vs
NSMutableArray *myArray = [#[] mutableCopy]
What are the differences between declaring arrays in these two separate ways?
Think of the 2nd line as:
NSMutableArray *myArray = [[[NSArray alloc] init] mutableCopy];
So the clear difference is the 1st way is more efficient and the 2nd way needlessly creates an extra object that is promptly discarded.

how to add object from one key NSDictionary in NSMutableArray

I have one NSDictionary and one NSMutableArray And I want store many object from NSDictionary into NSMutableArray from one key but I dont know about it.
this is my code :
//NSArray * a = [[NSArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"7",#"9", nil];
//NSArray *b = [[NSArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",#"0", nil];
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:a,#"number1",b,#"number2", nil];
NSMutableArray *array = [NSMutableArray alloc].... ?
I want store object in number1 key from NSDictionary in NSMutableArray
Since it appears that you store arrays as NSDictionary elements, you can do this:
NSMutableArray *array = [[NSMutableArray alloc] initiWithArray:dic[#"number1"]];
or
NSMutableArray *array = [NSMutableArray arrayWithArray:dic[#"number1"]];
To add object into NsDictionary Array do:
NSMutableDictionary * datos = [array objectAtIndex:i];
[datos setObject:#"hola" objectForKey#"newKey"];

Sort NSMutableArray memory leak

My object has a private NSMutableArray items. I am using the following code to sort the objects in items in size order:
-(void)sortItems{
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"size" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [items sortedArrayUsingDescriptors:sortDescriptors];
NSMutableArray* newArray = [[NSMutableArray alloc] initWithArray: sortedArray];
[self setItems:newArray];
[sortDescriptor release];
}
Obviously this is a memory leak here, because every time I call sortItems, I am allocing new memory and assigning items to point to it. I've tried releasing the old memory as follows:
NSMutableArray* newArray = [[NSMutableArray alloc] initWithArray: sortedArray];
NSMutableArray* oldArray = [self items];
[self setItems:newArray];
[oldArray release];
But that gives an EXC_BAD_ACCESS error. I've read up on memory handling in objC, and I'm convinced I'm doing something fundamentally wrong here.
Any help would be greatly appreciated!
You're leaking the new array, not the old one:
NSMutableArray* newArray = [[NSMutableArray alloc] initWithArray: sortedArray];
[self setItems:newArray];
[sortDescriptor release];
[newArray release]; // <-- add this
The fundamental rule is that you must release anything that you have allocated, and you normally shouldn't care about keeping things retained for anyone (i.e. [self setItems:]), those who need something retained will do it themselves.
I would also recommend making self.items a mutable array, and using [self.items sortUsingDescriptors:sortDescriptor to sort inplace without creating a copy.
Is there a reason why you cannot release the newArray in your first example?
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"size" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [items sortedArrayUsingDescriptors:sortDescriptors];
NSMutableArray* newArray = [[NSMutableArray alloc] initWithArray: sortedArray];
[self setItems:newArray];
[newArray release];
[sortDescriptor release];

Adding nil to NSMutableArray

I am trying to create a NSMutableArray by reading in a .txt file and am having trouble setting the last element of the array to nil.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"namelist" ofType:#"txt"];
NSString *data = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSArray *list = [data componentsSeparatedByString:#"\n"];
NSMutableArray *mutableList = [[NSMutableArray alloc] initWithArray:list];
I wanted to use NSMutableArray's function addObject, but that will not allow me to add nil. I also tried:
[mutableList addObject:[NSNull null]];
but that does not seem to work either. Is there a way around this problem?
Use
NSMutableArray *mutableList = [[NSMutableArray alloc] init];
[mutableList addObjectsFromArray:list];
Hope this helps
jrtc27
Per Apple's documentation on NSMutableArray.
addObject:
Inserts a given object at the end of the receiver.
- (void)addObject:(id)anObject
Parameters
anObject
The object to add to the end of the receiver's content. **This value must not be nil.**
Reference
http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

How to copy last object of array to other array?

how would you copy the last object of a array and then add the object to some array. So in other words i want to take the last object of someArray and copy that lastObject to someArray2.
Thanks,
-David
NSArray *firstArray = [[NSArray alloc] init];
... populate firstArray ...
NSArray *secondArray = [NSArray arrayWithObject:[firstArray lastObject]];
or
NSArray *firstArray = [[NSArray alloc] init];
... populate firstArray ...
NSMutableArray *secondArray = [NSMutableArray alloc] init];
[secondArray addObject:[firstArray lastObject]];
or
NSArray *firstArray = [[NSArray alloc] init];
... populate firstArray ...
NSArray *secondArray = [[NSArray alloc] init];
NSArray *thirdArray = [secondArray arrayByAddingObject:[firstArray lastObject]];
Make sure everything is released as you now own all these references.
Edit: If you want a COPY everywhere there's [firstArray lastObject] change it to [[[firstArray lastObject] copy] autorelease] (thanks tc)