将 NSDictionary 转换为 NSArray
Convert NSDictionary to NSArray
我正在尝试将我的 NSDictionary
(storageData) 转换为 NSArray
,因为我需要通过 segue 传递字典中包含的数据(标题)。我已经实现了下面的(见最后一个方法,tableview 只是为了提供一些上下文),但是发生崩溃并出现以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]:
unrecognized selector sent to instance
知道如何解决吗?
Viewcontrolller.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoctorsTableIdentifier = @"StorageItemTableViewCell";
StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StorageItemTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSDictionary *tmp = [self.storageData objectForKey:[NSString stringWithFormat:@"%d",(long)indexPath.row]];
[[cell itemName] setText:(NSString *)[tmp objectForKey:@"title"]];
NSString *title = [tmp objectForKey:@"title"];
[[cell itemName] setText:title];
NSDictionary *node = [self.descripData objectAtIndex:indexPath.row];
[[cell itemDescrip] setText:[node objectForKey:@"body"]];
NSString *secondLink = [[self.descripData objectAtIndex:indexPath.row] objectForKey:@"photo"];
[cell.itemPhoto sd_setImageWithURL:[NSURL URLWithString:secondLink]];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *values = [self.storageData allKeys];
ItemDetailViewController *detailViewController = [[ItemDetailViewController alloc]
initWithNibName:@"ItemDetailViewController" bundle:nil];
detailViewController.title = [[values objectAtIndex:indexPath.row] objectForKey:@"title"];
detailViewController.itemDetail = [self.descripData objectAtIndex:indexPath.row];
detailViewController.secondLink = self.descripData[indexPath.row][@"photo"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
错误表明您正在 NSString 类型的对象中调用 objectForKey,而该对象没有声明该方法。
所以在方法的开头打断点,逐行逐行直到崩溃,然后检查你认为是字典的变量是字符串。
我相信如果你要提取 allValues 而不是 allKeys 那么你只能:
[values objectAtIndex:indexPath.row]
因为您使用的索引是 indexPath.row。如果这有帮助,请告诉我。
我正在尝试将我的 NSDictionary
(storageData) 转换为 NSArray
,因为我需要通过 segue 传递字典中包含的数据(标题)。我已经实现了下面的(见最后一个方法,tableview 只是为了提供一些上下文),但是发生崩溃并出现以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance
知道如何解决吗?
Viewcontrolller.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DoctorsTableIdentifier = @"StorageItemTableViewCell";
StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StorageItemTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSDictionary *tmp = [self.storageData objectForKey:[NSString stringWithFormat:@"%d",(long)indexPath.row]];
[[cell itemName] setText:(NSString *)[tmp objectForKey:@"title"]];
NSString *title = [tmp objectForKey:@"title"];
[[cell itemName] setText:title];
NSDictionary *node = [self.descripData objectAtIndex:indexPath.row];
[[cell itemDescrip] setText:[node objectForKey:@"body"]];
NSString *secondLink = [[self.descripData objectAtIndex:indexPath.row] objectForKey:@"photo"];
[cell.itemPhoto sd_setImageWithURL:[NSURL URLWithString:secondLink]];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *values = [self.storageData allKeys];
ItemDetailViewController *detailViewController = [[ItemDetailViewController alloc]
initWithNibName:@"ItemDetailViewController" bundle:nil];
detailViewController.title = [[values objectAtIndex:indexPath.row] objectForKey:@"title"];
detailViewController.itemDetail = [self.descripData objectAtIndex:indexPath.row];
detailViewController.secondLink = self.descripData[indexPath.row][@"photo"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
错误表明您正在 NSString 类型的对象中调用 objectForKey,而该对象没有声明该方法。
所以在方法的开头打断点,逐行逐行直到崩溃,然后检查你认为是字典的变量是字符串。
我相信如果你要提取 allValues 而不是 allKeys 那么你只能:
[values objectAtIndex:indexPath.row]
因为您使用的索引是 indexPath.row。如果这有帮助,请告诉我。