我是Android开发人员现在切换到IOS,所以我是IOS开发的新手。 我错了这个地方
Thread1v:signal SIGABRT
。如何解决错误
-(void)updatePostCountUserDefaults{
NSMutableDictionary *postCount ;
NSNumber *count;
if([[NSUserDefaults standardUserDefaults] objectForKey:@"postCount"] !=nil){
postCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"postCount"];
count = [postCount objectForKey:@"count"];
count = [NSNumber numberWithInt:([count integerValue] + 1.0)];
int n = [count integerValue];
NSLog(@"hey count is : %i" , n);
[postCount setObject:count forKey:@"count"];
}else{
postCount = [[NSMutableDictionary alloc] init];
count = [NSNumber numberWithInt:1];
[postCount setObject:count forKey:@"count"]; //error-
}
[[NSUserDefaults standardUserDefaults] setObject:postCount forKey:@"postCount"];
[[NSUserDefaults standardUserDefaults] synchronize];
int num = [count integerValue];
NSLog(@"count is %i " , num);
if([count integerValue] == 4 || [count integerValue] == 12){
// show rating screen
}
请帮助
答案 0 :(得分:2)
实际上我认为你标记的行"错误"不会崩溃。它[postCount setObject:count forKey:@"count"];
将导致异常。因为该上下文中的postCount
是NSDictionary
,而不是可变字典。因此,它无法回复setObject:forKey:
。
编辑:正确的代码:
if([[NSUserDefaults standardUserDefaults] objectForKey:@"postCount"] !=nil){
postCount = [[[NSUserDefaults standardUserDefaults] objectForKey:@"postCount"] mutableCopy];
count = [postCount objectForKey:@"count"];
count = [NSNumber numberWithInt:([count integerValue] + 1.0)];
int n = [count integerValue];
NSLog(@"hey count is : %i" , n);
[postCount setObject:count forKey:@"count"];
}else{...}