Reputation: 121
I have an array (items) of custom class objects (catalogItem) each catalogItem has various properties one of which is an mastering called caption
I'm trying to update this catalogItem.caption in each catalogItem in the items array from nsstrings in the second array (tempCaption)
I know to iterate through the arrays but I can't seem to get the syntax right because what I seem to be doing is each catalogItem.caption cycles through each nsstring in the tempCaption array. So it iterates like 49 times instead of the 7 it should. catalogItem.caption all end up being the last item in the tempCaption array.
ViewController.m
-(void)parseUpdateCaptions
{
NSMutableArray *tempCaptions = [NSMutableArray array];
//get server objects
PFQuery *query = [PFQuery queryWithClassName:@"UserPhoto"];
NSArray* parseArray = [query findObjects];
//fast enum and grab strings and put into tempCaption array
for (PFObject *parseObject in parseArray) {
[tempCaptions addObject:[parseObject objectForKey:@"caption"]];
}
//fast enum through each array and put the capString into the catalogItem.caption slot
//this iterates too much, putting each string into each class object 7 times instead of just putting the nsstring at index 0 in the temCaption array into the catalogItem.caption at index 0, etc. (7 catalogItem objects in total in items array, and 7 nsstrings in tempCaption array)
for (catalogItem in items) {
for (NSString *capString in tempCaptions) {
catalogItem.caption = capString;
DLog(@"catalog: %@",catalogItem.caption);
}
}
}
if needed - class object header.h
#import <Foundation/Foundation.h>
@interface BBCatalogClass : NSObject
@property (nonatomic, strong) NSData *image;
@property (nonatomic, strong) NSData *carouselImage;
@property (nonatomic, strong) NSString *objectID;
@property (nonatomic, strong) NSString *caption;
@end
Upvotes: 0
Views: 118
Reputation: 18290
I would try traditional for looping instead of fast enumeration. This will work if I understand you correctly, that the indexes of the two arrays are aligned.
for(int i = 0; i<items.count; i++) {
catalogItem = [items objectAtIndex:i];
catalogItem.caption = [tempCaptions objectAtIndex:i];
}
Upvotes: 1
Reputation: 17186
You are iterating with nested for loop. It means total iteration count will be (items.count * tempCaptions.count).
So, either you should fast iterate both in a single loop or you should go with traditional approach as suggested above.
Upvotes: 0