i use this code to check if any objects exist in my NSMutableArray if yes i remove them all but it crashes although there are objects why?
if([NSMutableArray1 count]==1)
{
[poemoptionslist removeAllObjects];
}
if ([NSMutableArray1 count]==0)
{
[poemoptionslist addObject: final1];
}
控制台输出
2010-10-18 03:42:13.166 app1[33398:207] * Terminating app due to uncaught exception NSInternalInconsistencyException , reason: -[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object * Call stack at first throw: ( 0 CoreFoundation
0x02e55b99 exceptionPreprocess + 185 1 libobjc.A.dylib
0x02fa540e objc_exception_throw + 47 2 CoreFoundation
0x02e0e238 +[NSException raise:format:arguments:] + 136 3
CoreFoundation
0x02e0e1aa +[NSException raise:format:] + 58 4
CoreFoundation
0x02e4d3c1 -[__NSCFArray removeObjectAtIndex:] + 193 5
CoreFoundation
0x02dfe973 -[NSMutableArray removeAllObjects] + 83 6
poemsoflove
0x0004dc8d -[submitpoem submitpoem:] + 18560 7 UIKit
0x003b77f8 -[UIApplication sendAction:to:from:forEvent:] + 119 8 UIKit
0x00442de0 -[UIControl sendAction:to:forEvent:] + 67 9
UIKit
0x00445262 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 10 UIKit
0x00443e0f -[UIControl touchesEnded:withEvent:] + 458 11 UIKit
0x003db3d0 -[UIWindow _sendTouchesForEvent:] + 567 12 UIKit
0x003bccb4 -[UIApplication sendEvent:] + 447 13 UIKit 0x003c19bf _UIApplicationHandleEvent + 7672 14 GraphicsServices
0x033e6822 PurpleEventCallback + 1550 15 CoreFoundation
0x02e36ff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52 16 CoreFoundation 0x02d97807 __CFRunLoopDoSource1 + 215 17 CoreFoundation
0x02d94a93 __CFRunLoopRun + 979 18 CoreFoundation
0x02d94350 CFRunLoopRunSpecific + 208 19 CoreFoundation
0x02d94271 CFRunLoopRunInMode + 97 20 GraphicsServices
0x033e500c GSEventRunModal + 217 21 GraphicsServices
0x033e50d1 GSEventRun + 115 22 UIKit 0x003c5af2 UIApplicationMain + 1160 23 poemsoflove
0x00002728 main + 102 24 poemsoflove 0x000026b9 start + 53 25 ???
0x00000001 0x0 + 1 ) terminate called after throwing an instance of NSException Program received signal: “SIGABRT”.
伙计们,这里没有NSArray!
我保存到NSUSerDefault,如下所示:
if ([mutable1 count]==0) { [mutable1 addObject: final1]; }
NSUserDefaults *list =[NSUserDefaults standardUserDefaults];
[list setObject:mutable1 forKey:@"favorites"];
[list synchronize];
我这样加载数据
NSUserDefaults *prefs1 =[NSUserDefaults standardUserDefaults];
if ( [prefs1 objectForKey:@"favorites"] != nil)
{
mutable1 = [[NSMutableArray alloc] init];
mutable1 = [prefs1 objectForKey:@"favorites"];
我得到了这些东西!然后当它运行removeallobjects时,它会崩溃!