我有array
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];
当我尝试更改布尔值时,这会导致错误,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"]];
}
}
上面代码中可能出现什么问题? 谢谢,
空间
答案 0 :(得分:3)
Objective-C集合类只能保存对象,而不能保存基本类型,因此请更改:
BOOL isFileChoosen=YES;
要:
NSNumber *isFileChosen = [NSNumber numberWithBool:YES];
为了在对象中有效地包装你的布尔值。
(当然,这会对代码的其他部分产生影响)。