【问题标题】:why App crashes getting data from Dictionary?为什么应用程序从字典中获取数据会崩溃?
【发布时间】:2012-03-21 13:00:21
【问题描述】:

我正在开发一个应用程序,我必须在字典中保存一些数据,然后我必须加载该数据并向用户显示用户保存的任何选项。 当我从字典中获取数据时,它第二次而不是第一次崩溃,并且每次都给出不同的崩溃日志。 喜欢

-[UIDeviceRGBColor objectAtIndex:]: unrecognized selector sent to instance 0x68c22d0

[UIControlTargetAction count]: unrecognized selector sent to instance 0x68683d0

   0 : <CFString 0x687cb30 [0x124eb38]>{contents = "dish1"} = UIDeviceWhiteColorSpace 0 0.5

我的代码是这样的

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == menuTableView) {

        [optionsView addSubview:topScrollView];
        optionFlag=NO;
        [self optionScreenMethod:indexPath.row];
        NSString *dishSelect=[NSString stringWithFormat:@"dish%d",indexPath.row];
        NSMutableArray *dishesArray=[[NSMutableArray alloc]init];
        NSMutableArray *dishDicArray=[[NSMutableArray alloc]init];
        dishesArray=[dishDictionary objectForKey:dishSelect];
        NSLog(@"%d",[dishesArray count]);
        if ([dishesArray count]>0) {
            optionFlag=YES;
        }else{
            for (int i=0; i<[tempStr intValue]; i++) {
                DishData *dishData=[[DishData alloc]init];
                dishData.dishTitle=(NSMutableString*)[[tableMenuArray objectAtIndex:indexPath.row] itemName];
                dishData.accompArrayIndex=nil;
                dishData.cookArrayIndex=nil;
                dishData.dishComent=nil;
                dishData.nonMandArray=nil;
                [dishDicArray addObject:dishData];
            }
            [dishDictionary setObject:dishDicArray forKey:dishSelect];
        }
        //[dishesArray release];
        //[dishDicArray release];
        dishSelectRow=indexPath.row;
      //  NSString *dishSelect=[NSString stringWithFormat:@"dish%d",indexPath.row];
        
        [isSelected removeAllObjects];
        [isSelectedAccompArray removeAllObjects];
        [isSelectedCookArray removeAllObjects];
        [self defaultDataArray];
        [accompmentTblView reloadData];
        [cookingTblView reloadData];
        [nonMandTblView reloadData];
        [nonMandtSelectOptionArray removeAllObjects];

        optionsView.hidden=NO;

    }

【问题讨论】:

  • 你在哪里定义“dishDictionary”?
  • @Mashhadi 我已经编辑了答案,请查看。希望对您有所帮助。

标签: iphone objective-c crash nsdictionary


【解决方案1】:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

       //If possible make a singleton instance here of "dishDictionary" may solve your issue of crashing (Rather diclare it at the class level)
      //if(!dishDictionary)
          dishDictionary=[[NSMutableDictionary alloc]init];

      ..........

      //Replace this line :
      NSMutableArray *dishDicArray=[[NSMutableArray alloc]init];

      to

      NSMutableArray *dishesArray=[[NSMutableArray alloc]initWithArray:[dishDictionary objectForKey:dishSelect]];

      and remove
      dishesArray=[dishDictionary objectForKey:dishSelect];

      ..........
      //may will not crash than try this
}

【讨论】:

  • 感谢它的工作,但是当此代码在此行第 3 次运行时它再次崩溃 NSMutableArray *dishesArray=[[NSMutableArray alloc]initWithArray:[dishDictionary objectForKey:dishSelect]];
  • 我建议在调用 didselect 时分配。尝试编辑后的代码,我只是从代码中注释掉 1 行。如果它比 plg 效果好,请接受答案为真 :) 而只是评论答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-03
  • 1970-01-01
  • 2012-01-24
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多