【问题标题】:EXC_BAD_ACCESS when changing bool value in nsdictionary更改 nsdictionary 中的布尔值时的 EXC_BAD_ACCESS
【发布时间】:2013-07-05 19:11:51
【问题描述】:

我有一个arraynsdictionaries,每个字典都有一个boolean 值,我想在用户选择行时更改boolean 值。

但不知怎的,我得到了exc_bad_access

这是设置字典数组的方式:

self.choosedfiles=[[NSMutableArray alloc] init];
for (NSString *s in fileListINDoc){
        NSArray *partialDates =[s componentsSeparatedByString:@"."];//split string where - chars are found
        NSFileManager* fm = [NSFileManager defaultManager];
        NSDictionary* attrs = [fm attributesOfItemAtPath:[dataPath stringByAppendingPathComponent:s] error:nil];
        NSDate *dateLocal =(NSDate*)[attrs objectForKey: NSFileModificationDate];

        //creatre unique name for image _AKIAIVLFQKMSRN5JLZJA
        NSString *uniqueFileName=[NSString stringWithFormat:@"%@_AKIAIVLFQKMSRN5JLZJA_%@.jpeg",[partialDates objectAtIndex: 0],[partialDates objectAtIndex: 1]];

        BOOL isFileChoosen=NO;

        NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                                dateLocal,@"date",uniqueFileName,@"imagename",s,@"name",isFileChoosen,@"isFileChoosen",
                                nil];
        [tempArray addObject:params];
    }
    NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
    NSArray *descriptors=[NSArray arrayWithObject: descriptor];
    NSArray *reverseOrder=[tempArray sortedArrayUsingDescriptors:descriptors];

    self.files=[[NSMutableArray alloc] initWithArray:reverseOrder];

当我尝试更改布尔值时,这会导致错误,XCODE 将行 *params 字典显示为错误:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSDictionary *currentArticle = [self.files objectAtIndex:indexPath.row];
   NSNumber* ischoosenObject =[currentArticle objectForKey:@"isFileChoosen"];
    if ([ischoosenObject boolValue]==NO) {
        BOOL isFileChoosen=YES;
        NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:[currentArticle objectForKey:@"date"],@"date",[currentArticle objectForKey:@"imagename"],@"imagename",[currentArticle objectForKey:@"name"],@"name",isFileChoosen,@"isFileChoosen",nil];
        [self.files replaceObjectAtIndex:indexPath.row withObject:params];
        [self.choosedfiles addObject:[currentArticle objectForKey:@"name"]];


    }
}

上面的代码可能是什么问题? 谢谢,

空格

【问题讨论】:

    标签: ios objective-c boolean nsdictionary


    【解决方案1】:

    Objective-C 集合类只能保存对象,不能保存原始类型,所以改一下:

    BOOL isFileChoosen=YES;
    

    收件人:

    NSNumber *isFileChosen = [NSNumber numberWithBool:YES];
    

    为了有效地包装你的布尔值在一个对象中。

    (当然,这会对代码的其他部分产生一些影响)。

    【讨论】:

    • 感谢您的回答,但是为什么当我在 for 循环中设置字典但在 didSelectRowAtIndexPath 中不起作用时,`BOOL isFileChoosen=NO;` 有效?
    • @SpaceDust 抱歉,我不知道为什么会这样。不过,我确信它会在某个时候中断。
    猜你喜欢
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 2014-01-06
    • 2012-01-09
    • 2012-10-29
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    相关资源
    最近更新 更多