Encephalon Encephalon - 11 months ago 79
iOS Question

Loop through NSStrings in NSArray

Basically what I want to do is this:
1. Grab .plist from URL
2. Put array from plist file into an NSMutableArray
3. Loop through each string in the NSMutableArray and sort them into multiple NSMutableArrays

I've already done 1 & 2 and I have an NSMutableArray good to go.
So this is what strings look like from the NSArray:

Looper|September 28th, 2012|http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4?TestLink|http://ia.media-imdb.com/images/M/MV5BMTM5NTkwMzI2MF5BMl5BanBnXkFtZTcwMTc5MjQ2Nw@@._V1._SY317_.jpg


This is a movie trailers app so what I want to be able to do is sort this string (and all other strings that are formatted similarly in the NSMutabeArray) into 4 different NSMutableArrays, and split them at the "|" using componentsSeperatedByString. This is what I have so far but when I log "titleArray" it only comes up with the first title in the array:

NSInteger count = [newTrailers count];
for (int i = 0; i < count; i++) {
NSString* body = [newTrailers objectAtIndex:i];

NSArray *splits = [NSArray arrayWithObjects:body, nil];

NSMutableArray* titleArray = [[NSMutableArray alloc] init];
NSMutableArray* descriptionArray = [[NSMutableArray alloc] init];
NSMutableArray* linkArray = [[NSMutableArray alloc] init];
NSMutableArray* posterArray = [[NSMutableArray alloc] init];

for (NSString* item in splits)
{
NSArray* parts = [item componentsSeparatedByString: @"|"];
if ([parts count] == 4)
{
[titleArray addObject: [parts objectAtIndex: 0]];
[descriptionArray addObject: [parts objectAtIndex: 1]];
[linkArray addObject: [parts objectAtIndex: 2]];
[posterArray addObject: [parts objectAtIndex: 3]];
}
}

NSLog(@"Title Arrray: %@", titleArray);
}


Thanks for any help and I am new to for loops and ints, so please be easy on me!

Answer Source

get rid of the splits array and its forin loop and just split the body string directly:

NSString* body = [newTrailers objectAtIndex:i];
NSMutableArray* titleArray = [[NSMutableArray alloc] init];
NSMutableArray* descriptionArray = [[NSMutableArray alloc] init];
NSMutableArray* linkArray = [[NSMutableArray alloc] init];
NSMutableArray* posterArray = [[NSMutableArray alloc] init];

NSArray* parts = [body componentsSeparatedByString: @"|"];
if ([parts count] == 4) {

    [titleArray addObject: [parts objectAtIndex: 0]];
    [descriptionArray addObject: [parts objectAtIndex: 1]];
    [linkArray addObject: [parts objectAtIndex: 2]];
    [posterArray addObject: [parts objectAtIndex: 3]];
}