NSString进入NSArray

Sam*_*ish 4 iphone xcode nsstring nsarray

我目前正在接收一个文件并将其存储到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"];
Run Code Online (Sandbox Code Playgroud)

它显示数据为:

CompanyName | AccountCode CompanyName | AccountCode CompanyName | AccountCode

我的问题是:我可以将"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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

出于测试目的,我的代码是:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

BP.*_*BP. 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"]);
Run Code Online (Sandbox Code Playgroud)

日志代码显示生成的字典以及如何根据公司名称提取对象.请记住,此处没有错误检查,如果其中一个数组组件中没有管道符,则setObject行将会爆炸.

当然,如果您希望将帐户代码作为密钥,只需在setObject行中翻转1和0即可.

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

cell.textLabel.text = [[dict allKeys] objectAtIndex:[indexPath row]];
cell.detailTextLabel.text = [dict objectForKey:cell.textLabel.text];
Run Code Online (Sandbox Code Playgroud)

(我希望这是对的,我没办法在Xcode中立即测试它.)