简体   繁体   English

从NSMutable数组解析/提取和合并字符串

[英]Parse / Extract & Combine Strings from NSMutable Array

Am trying to figure out how to do this.... 我正在尝试弄清楚该如何做。

I have an NSMutable array populated from an RSS feed as follows : 我有一个从RSS feed填充的NSMutable数组,如下所示:

Item 1 (date, title, description) 项目1(日期,标题,说明)

Item 2 (date, title, description) 项目2(日期,标题,说明)

Item 3 (date, title, description) 项目3(日期,标题,说明)

etc.... 等等....

What I'm trying to do, is loop through it and create a string for use in an animated ticker as follows : 我正在尝试做的是,遍历它并创建一个字符串,以供在动画股票中使用,如下所示:

Item 1 Date - Item 1 Title [a few spaces] Item 2 Date - Item 2 Title [a few spaces] etc..... 项目1日期-项目1标题[少数空格]项目2日期-项目2标题[少数空格]等.....

I understand how to extract a single element from the array - for example : 我了解如何从数组中提取单个元素-例如:

NSString *testString1 = [[_parseResults objectAtIndex:0] objectForKey:@"date"];
NSString *testString2 = [[_parseResults objectAtIndex:0] objectForKey:@"title"];

but am stumped as to the best way to loop through the whole thing and construct the combined string - ie Item 1 Date + "-" + Item 1 Title + Item 2 Date + "-" + Item 2 Title etc ? 但是对于遍历整个事物并构造组合字符串的最佳方式感到困惑,即项目1日期+“-” +项目1标题+项目2日期+“-” +项目2标题等?

If you want to loop over them, an easy way might something like this: 如果要遍历它们,一种简单的方法可能是这样的:

NSString* finalStr = nil;
for (int i = 0; i < [_parseResults count]; i++)
{
    NSDictionary* nextItem = [_parseResults objectAtIndex:i];
    NSString* dateStr = [nextItem objectForKey:@"date"]; // You might want to make the keys be named constants
    NSString* titleStr = [nextItem objectForKey:@"title"];
    ...
    finalStr = [finalStr stringByAppendingFormat:@"\t%@-%@", dateStr, titleStr];
}

At the end, finalStr will contain the whole string you want. 最后, finalStr将包含您想要的整个字符串。

You may use fast enumeration, let compiler choose the optimal way to go through your array elements. 您可以使用快速枚举,让编译器选择遍历数组元素的最佳方法。

for(NSDictionary *rssItem in _parseResults)
{
    NSString* dateString = [rssItem objectForKey:@"date"];
    NSString* titleString = [rssItem objectForKey:@"title"];
    result = [result stringByAppendingFormat:@" %@-%@", dateString, titleString];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM