将NSString转换成NSArray

4

我目前正在接收一个文件并将其存储到NSString中。然后,我将字符串创建为数组,并在TableView中呈现它。这在某种程度上起作用。我目前接收的数据格式如下:

公司名称|账户代码\r\n公司名称|账户代码\r\n公司名称|账户代码\r\n等等...

目前我正在做:

NSString *dataString = [NSString stringWithContentsOfURL:url encoding:nil error:nil];
myArray = [dataString componentsSeparatedByString:@"\r\n"];

这段代码展示的数据如下:

公司名称|账号编码 公司名称|账号编码 公司名称|账号编码

我的问题是:我能将"dataString"拆分为二维数组吗?还是应该创建两个数组(一个包含公司名称,一个包含账号编码)?如何操作?

谢谢。

编辑:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";  

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    if (testArray.count >indexPath.row) {

        cell.textLabel.text = [testArray2 objectAtIndex:0];
        cell.detailTextLabel.text = [testArray2 objectAtIndex:1];

    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

编辑:

为了测试目的,我的代码是:

- (void)viewDidLoad {
    [super viewDidLoad];

    testArray = [[NSArray alloc] init];
    NSString *testString = @"Sam|26,Hannah|22,Adam|30,Carlie|32";
    testArray = [testString componentsSeparatedByString:@","];

    dict = [NSMutableDictionary dictionary];
    for (NSString *s in testArray) {

        testArray2 = [s componentsSeparatedByString:@"|"];
        [dict setObject:[testArray2 objectAtIndex:1] forKey:[testArray2 objectAtIndex:0]];
    }

    NSLog(@"Dictionary: %@", [dict description]);
    NSLog(@"Account code for CompanyName1: %@", [dict objectForKey:@"CompanyName1"]);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";  

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    if (testArray.count >indexPath.row) {

        cell.textLabel.text = [testArray2 objectAtIndex:0];
        cell.detailTextLabel.text = [testArray2 objectAtIndex:1];

    }

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}
2个回答

6

Marzapower是正确的,您可能需要使用NSDictionary或NSMutableDictionary来存储键值对。在上面的代码正下方,您可以使用以下代码:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *s in myArray)
{
    NSArray *arr = [s componentsSeparatedByString:@"|"];
    [dict setObject:[arr objectAtIndex:1] forKey:[arr objectAtIndex:0]];
}
NSLog(@"Dictionary: %@", [dict description]);
NSLog(@"Account code for CompanyName1: %@", [dict objectForKey:@"CompanyName1"]);

日志代码展示了生成的字典以及如何根据公司名称提取对象。请注意,这里没有错误检查,如果数组组件中没有管道字符,setObject行将会出错。

当然,如果您想要账户代码作为键,只需在setObject行中翻转1和0即可。

编辑:在cellForRowAtIndexPath中,您应该访问dict字典而不是testArray2数组。例如,您可能想要执行以下操作:

cell.textLabel.text = [[dict allKeys] objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = [dict objectForKey:cell.textLabel.text];

我希望这是正确的,我没有办法立即在Xcode中测试它。


这个程序现在(几乎)可以工作了 - 表格视图现在会显示最后一个CompanyName和AccountCode 4次(字符串中公司的数量)。 - Sam Parrish
当我运行NSLog(@"Dictionary: %@", [dict description]);时,它显示了正确的数据,但在显示数据时出现了上述问题... - Sam Parrish
字典中的键必须是唯一的,以创建不同的条目。如果在 setObject 调用中重复使用相同的键,则只会替换字典中已存在的值。 - BP.
实际上,这听起来像是在生成单元格时出现了代码问题。您能否编辑原始问题并包括您的cellForRowAtIndexPath方法? - BP.
我该如何为未知数量的条目指定不同的键? - Sam Parrish
显示剩余3条评论

1
你可以创建一个关联数组,也称为“字典”。使用字典,你可以将一个字符串(值)与另一个字符串(键)相关联。这样,你就可以得到类似于以下的结果:
"Company1" => "account code 1",
"Company2" => "account code 2",
...

接下来你可以使用allKeys方法遍历它的键。欲了解更多信息,请参阅NSMutableDictionary文档。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接