Reputation: 1493
I have two arrays fetch from web servers, both only contains same numbers of dictionaries. i want to parse every dictionary in two nested for each loops, so used following code:
for (NSDictionary *subscription in feedsArray)
{
for (NSDictionary *unreadCount in unreadCountsArray)
{
[RDInsertObjects insertFeedsWithSubscription:subscription
unreadCount:unreadCount];
}
}
But the results returned isn't me wanted, the inner for loop seemed executed over many times, anyone can tell me how exactly do right things to make this code make sense?
Upvotes: 0
Views: 2091
Reputation: 4520
The way you do it, for each single iteration of the first loop the second loop iterates completely.
I think it won't work with a for-in loop. But you said the number of items in the two arrays is the same - so you can easily do:
for (i=0; i< feedsArray.count; i++) {
NSDictionary subscription = feedsArray[i];
NSDictionary unreadCount = unreadCountsArray[i];
[RDInsertObjects insertFeedsWithSubscription: subscription unreadCount: unreadCount];
}
This will only work, if the number of items in the array are always ever the same - if sometime they aren't, you need a modification to prevent an out-of-bounds error or it will crash!
Upvotes: 2