xcode NSString 到 NSArray
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6293445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSString into NSArray
提问by Sam Parrish
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:
我目前正在接收一个文件并将其存储到 NSString 中。然后我从字符串中创建一个数组并将其呈现在 TableView 中。这在一定程度上起作用。我目前正在接收这样的数据:
CompanyName|AccountCode\r\nCompanyName|AccountCode\r\nCompanyName|AccountCode\r\n and so on...
CompanyName|AccountCode\r\nCompanyName|AccountCode\r\nCompanyName|AccountCode\r\n 等等...
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?
我的问题是:我可以将“dataString”拆分为二维数组吗?或者我应该创建 2 个数组(一个带有 CompanyName,一个带有 AccountCode)。我该怎么做?
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;
}
回答by BP.
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:
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"]);
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.
日志代码显示了生成的字典以及如何根据公司名称提取对象。请记住,这里没有错误检查,如果数组组件之一中没有管道字符,则 setObject 行将炸毁。
And of course if you want the account code as the key, just flip around the 1 and 0 in the setObject line.
当然,如果您想要帐户代码作为键,只需在 setObject 行中翻转 1 和 0 即可。
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:
编辑:在 cellForRowAtIndexPath 中,您应该访问 dict 字典而不是 testArray2 数组。例如,你可能想要做这样的事情:
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.)
(我希望这是正确的,我没有办法立即在 Xcode 中对其进行测试。)
回答by marzapower
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.
然后您可以使用该allKeys
方法迭代其键。请参阅NSMutableDictionary
文档以获取更多信息。