2

基本的に私がやりたいことはこれです:1。URLから.plistを取得します2.plistファイルからNSMutableArrayに配列を置きます3.NSMutableArrayの各文字列をループし、それらを複数のNSMutableArrayにソートします

私はすでに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に並べ替え、「|」で分割することです。componentsSeperatedByStringを使用します。これは私がこれまでに持っているものですが、「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ループとintに慣れていないので、気楽にやってください!

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 に答える