2012-08-22 38 views
1
NSString的

基本上就是我想要做的是这样的:从plist文件URL 2.将阵列 1.拿的.plist到一个NSMutableArray 三环路穿过的NSMutableArray每个字符串,并将其整理成多个NSMutableArrays遍历的NSArray

我已经完成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/[email protected]@._V1._SY317_.jpg 

这是一个电影预告片应用程序,所以我希望能够做的是排序此字符串(和那些在NSMutabeArray类似格式的所有其他字符串)分成4个不同的NSMutableArrays,并将它们分割为“|”使用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); 
} 

感谢您的帮助,我是新来的循环和整型,所以请容易在我身上!

+0

代码中将字符串

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

数组的示例代码看起来不错,在NSLog的阵列newTrailers的计数和张贴在这里。 –

+0

根据你发布的字符串模板,'parts count'应该是3而不是4 ... – KDaker

+1

如果你的意思是在数组外使用titleArray(和其他),它们需要在数组外部初始化。否则,在每次循环迭代中覆盖它们。 – LordTwaroog

回答

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]]; 
} 
+0

再次感谢KDaker! – Encephalon

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

这里是在迅速

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

for str in arr as! [String] 
{ 

    print(str) 
}