2

基本上我想要做的是: 1. 从 URL 获取 .plist 2. 将数组从 plist 文件放入 NSMutableArray 3. 遍历 NSMutableArray 中的每个字符串并将它们排序为多个 NSMutableArrays

我已经完成了 1 和 2 并且我有一个 NSMutableArray 可以使用。所以这就是 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

这是一个电影预告片应用程序,所以我想要做的就是将此字符串(以及在 NSMutabeArray 中格式类似的所有其他字符串)排序为 4 个不同的 NSMutableArray,并将它们拆分为“|” 使用 componentsSeparatedByString。这是我到目前为止所拥有的,但是当我记录“titleArray”时,它只会出现数组中的第一个标题:

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);
}

感谢您的帮助,我是 for 循环和整数的新手,所以请放轻松!

4

3 回答 3

1

在您的代码中创建了很多数组 - 特别是对于 newTrailers 数组中的每个条目,您为标题、描述等创建数组...

如果我说对了,那么您想做的事情应该是这样的(未经测试,只是对您的代码进行了一些重新排列):

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

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

    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]];
    }
}
NSLog(@"Title Arrray: %@", titleArray);
于 2012-08-22T22:02:34.540 回答
1

这是用于迭代字符串数组的示例代码

NSArray *arr = [NSArray arrayWithObjects:@"AString",@"AnotherString", nil];
for(NSString *str in arr){

    NSLog(@"%@",str);
}

迅速

let arr : NSMutableArray = ["A","B"]

for str in arr as! [String]
{

    print(str)
}
于 2016-11-16T11:29:44.267 回答
0

摆脱splits数组及其forin循环,直接拆分body字符串:

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]];
}
于 2012-08-22T22:18:31.597 回答