基本上我想做的是: 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个不同的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);
}
感谢您提供任何帮助,我是for循环和整体的新手,所以请对我这么轻松!
答案 0 :(得分: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);
答案 1 :(得分: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]];
}
答案 2 :(得分:0)
以下是迭代字符串数组
的示例代码NSArray *arr = [NSArray arrayWithObjects:@"AString",@"AnotherString", nil];
for(NSString *str in arr){
NSLog(@"%@",str);
}
in swift
let arr : NSMutableArray = ["A","B"]
for str in arr as! [String]
{
print(str)
}