0

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

2 Answers 2

1

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];
}
Sign up to request clarification or add additional context in comments.

2 Comments

this method worked except i had to change [array objectatindex:i].caption to catalogItem = [items objectAtIndex:i]; catalogItem.caption = [tempCaptions objectAtIndex:i];
Cool, expected. I have updated the answer so anyone who stumbles upon this will see it clearly.
0

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.

1 Comment

out of curiosity how would one do this using fast enumeration in a single loop?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.