【问题标题】:NSString into NSArrayNSString 转换为 NSArray
【发布时间】:2011-06-09 13:23:17
【问题描述】:

我目前正在接收一个文件并将其存储到一个 NSString 中。然后我从字符串中创建一个数组并将其呈现在 TableView 中。这在一定程度上起作用。我目前正在接收这样的数据:

CompanyName|AccountCode\r\nCompanyName|AccountCode\r\nCompanyName|AccountCode\r\n 等等...

目前我正在做:

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

将数据显示为:

公司名称|帐户代码 公司名称|帐户代码 公司名称|帐户代码

我的问题是:我可以将“dataString”拆分为二维数组吗?或者我应该创建 2 个数组(一个带有 CompanyName,一个带有 AccountCode)。我该怎么做?

谢谢

编辑:

// 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;
}

【问题讨论】:

    标签: iphone xcode nsstring nsarray


    【解决方案1】:

    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 次(字符串中的公司数量)
    • 当我运行 NSLog(@"Dictionary: %@", [dict description]);它显示了正确的数据,但是显示数据时出现上述问题...
    • 键的项目需要唯一才能在字典中创建不同的条目。如果在 setObject 调用中重复使用相同的键,它只会替换字典中已经存在的键的值。
    • 实际上,当您生成单元格时,这听起来像是您的代码有问题。您可以编辑原始问题并包含您的 cellForRowAtIndexPath 方法吗?
    • 如何为未知数量的条目指定不同的键?
    【解决方案2】:

    您可以创建一个关联数组,也称为“字典”。使用字典,您可以将字符串(值)关联到另一个(键)。这样你会得到这样的东西:

    "Company1" => "account code 1",
    "Company2" => "account code 2",
    ...
    

    然后您可以使用allKeys 方法遍历其键。 请参阅NSMutableDictionary 文档了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      相关资源
      最近更新 更多