ios - 更改 nsdictionary 中的 bool 值时的 EXC_BAD_ACCESS

标签 ios objective-c boolean nsdictionary

我有一个nsdictionaries数组,每个字典都有一个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];

当我尝试更改 boolean 值时,这会导致错误,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"]];


    }
}

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

空间

最佳答案

Objective-C 集合类只能保存对象,不能保存原始类型,因此需要更改:

BOOL isFileChoosen=YES;

致:

NSNumber *isFileChosen = [NSNumber numberWithBool:YES];

为了有效地将 boolean 值包装到一个对象中。

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

关于ios - 更改 nsdictionary 中的 bool 值时的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17526883/

相关文章:

Java 切换 boolean 值不起作用

boolean 值与数字比较中的 JavaScript 真实性

ios - 新创建的 NSManagedObject 即使在保存后也会返回临时 objectID

iphone - 更改搜索栏的背景图片

objective-c - 在子类中重铸属性

iphone - 删除按钮按下时的 MKMapView 覆盖

java - 为什么 Java 中的 boolean 值不支持类型转换

iOS - 归档错误 : no unexpired provisioning profiles that contain any of the keychains signing certificates

ios - Storyboard中 UITableViewCell 的 backroundView 属性设置运行时错误

ios - Xcode 4.5 - CopyPNGFile 仍然存在。需要新的想法