Reputation: 2552
I have a number of objects in NSArray. I want to reconstract NSMutableArray of NSMutableArrays with 6 elements in each out of NSArray. How can I do it?
Edit My code:
NSArray *newsList = [mynode6 findChildTags:@"td"]; //result of site parsing
NSMutableArray *someArray = [[NSMutableArray alloc] init];
int i=0;
for (HTMLNode *news in newsList) {
[someArray addObject:[newsList objectAtIndex:news];
i++;
if (i==5) {
i=0;
[self.valueArray addObject:someArray];
[someArray removeAllObjects];
}
}
valueArray
is my main NSMutableArray. This code does not work, valueArray
is empty.
Upvotes: 0
Views: 295
Reputation: 820
I couldn't understand this line.This line is not meaningful as objectAtIndex takes NSInteger as param..I presume ur trying to add HTMLNode element in that array
[someArray addObject:[newsList objectAtIndex:news];
So,I think u need to replace the above line like this..
[someArray addObject:news];
I have edited ur code snippet by making a few changes and i have plugged in my logic(Have created a new newsList array with some dummy objects to explain u the logic)..
Here goes the edited code
NSArray *newsList = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"A",@"B",@"C",@"D",@"E",@"F", nil];
NSMutableArray *valueArray = [[NSMutableArray alloc] init];
NSMutableArray *someArray = [[NSMutableArray alloc] init];
int i=0;
for (NSString *news in newsList) {
[someArray addObject:news];
i++;
if (i==6) {
i=0;
[valueArray addObject:someArray];
NSLog(@"%@",someArray);
[someArray release];
someArray = [NSMutableArray new];
}
}
NSLog(@"%@",valueArray);
Here is the output from terminal..
Hope it helped!! Have a good reading!
Upvotes: 1
Reputation: 4438
It sounds like your approach is too obtuse for your average programmer...but alas...
Do you really want to reuse someArray
in the manner you have listed above? Adding someArray
as an object of valueArray
and then removing all objects later will just leave your valueArray
full of empty arrays. Worse, you are adding the same array instance every single time...
Maybe instead of [someArray removeAllObjects]
you just make a new instance someArray = [[NSMutableArray alloc] init]
Then after your for loop, make sure to add whatever is left over in someArray
:
if(i) {
[self.valueArray addObject:someArray];
}
Upvotes: 1
Reputation: 2912
I had a very similar issue with empty arrays after doing something similar. At the point you add the array in make it look like this..
[self.valueArray addObject:[NSMutableArray arrayWithArray:someArray]];
This worked for me! Its because you add a reference to someArray then remove all the objects hence its empty. By additing it arrayWithArray you make a copy of the data so removing the rows form one leaves them in the other. :)
Plasma
Upvotes: 1
Reputation: 1893
Quick fix:
Allocate the array within your loop:
NSArray *newsList = [mynode6 findChildTags:@"td"]; //result of site parsing
NSMutableArray *someArray = [[NSMutableArray alloc] init];
int i=0;
for (HTMLNode *news in newsList) {
[someArray addObject:[newsList objectAtIndex:news];
if (++i == 5) {
i = 0;
[self.valueArray addObject:someArray];
[someArray release];
someArray = [[NSMutableArray alloc] init];
}
}
[someArray release];
Upvotes: 1