NSString 转 NSArray

发布于 2024-11-14 19:56:08 字数 2720 浏览 4 评论 0原文

我当前正在接收一个文件并将其存储到 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"];

将数据显示为:

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

编辑:

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

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

I am currently receiving a file and storing it into an NSString. I am then creating an array out of the string and presenting it in a TableView. This works to a certain extent. I am currently receiving data like this:

CompanyName|AccountCode\r\nCompanyName|AccountCode\r\nCompanyName|AccountCode\r\n and so on...

at the moment i am doing:

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

which displays data as:

CompanyName|AccountCode
CompanyName|AccountCode
CompanyName|AccountCode

My question is: Can i split the "dataString" into a 2 dimensional array? or should i create 2 arrays (one with the CompanyName, one with AccountCode). And how do i do that?

Thanks

EDIT:

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

EDIT:

for test purposes my code is:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴我心暖 2024-11-21 19:56:08

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 中测试它。)

Marzapower is correct, you are probably going to want to use an NSDictionary or NSMutableDictionary to store key value pairs. Right below your code above, you could use this code:

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"]);

The logging code shows the resulting dictionary as well as how to extract an object based on a company name. Please keep in mind that there is no error checking here, if there is no pipe character in one of the array components the setObject line will blow up.

And of course if you want the account code as the key, just flip around the 1 and 0 in the setObject line.

EDIT: In the cellForRowAtIndexPath you should be accessing the dict dictionary instead of the testArray2 array. For example, you would probably want to do something like this:

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

(I hope this is right, I do not have a way to immediately test it in Xcode.)

可是我不能没有你 2024-11-21 19:56:08

您可以创建关联数组,也称为“字典”。使用字典,您可以将一个字符串(值)与另一个字符串(键)关联起来。这样您将获得如下内容:

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

然后您可以使用 allKeys 方法。
请参阅 NSMutableDictionary 文档以获取更多信息。

You can create an associative array, also called "dictionary". With the dictionary you can associate a string (value) to another (key). This way you will obtain something like this:

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

Then you could iterate through its keys with the allKeys method.
Please, refer to the NSMutableDictionary documentation for further information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文