Here I am having a situation, I’m using the following code:
int x=0;
for (int i=0; i<=[arrayDeals count]-1; i++) {
x++;
//NSString *deal = [arrayDeals objectAtIndex:i];
combinedArr = [[NSMutableArray alloc]initWithObjects:
[CustomObject customObjectWithName:[arrayDeals objectAtIndex:i] andNumber:x],nil];
}
I need to load the values from arrayDeals and the ‘x’ value into combinedArr. So, I put this in a for loop. But i got only one value from each arrays. What is went wrong here? Please help me. (here CustomObject is a NSObject)
Thank you.
Well there are many things wrong with the code you posted, but I think this is what you want:
To give you some idea of what is wrong with the code you posted:
Here you create a new
NSMutableArrayto which you assign an new object to taked the object from the arrayarrayDeals. But you create thisNSMutableArrayfor every item in the arrayarrayDealsand you assign them to the same variable.So each iteration you leak the
NSMutableArray.Also :
is the same as
but the count is called every time you iterate, so as per my example I saved the count in a int to just speed things up.
You could even speed the code up using fast Enumeration: