English 中文(简体)
使我的应用程序[NSMutableArray1 removeAllObjects]iphone-sdk崩溃
原标题:crashes my app [NSMutableArray1 removeAllObjects] iphone sdk

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时,它会崩溃!

最佳回答
mutable1 = [[NSMutableArray alloc] init];
mutable1 = [prefs1 objectForKey:@"favorites"];

即使您已经将mutable1声明为NSMutableArray,您也要将其重新分配给NSUserDefaults对象返回的对象。这个对象显然是一个NSArray,而不是NSMutableArray,因此发生了崩溃。

通过执行以下操作,您可以使用首选项数组加载NSMutableArray:

mutable1 = [[NSMutableArray alloc] init];
[mutable1 addObjectsFromArray:[prefs1 objectForKey:@"favorites"]];
问题回答

错误消息表明您正在将消息发送到一个不可变的数组,这会引发一个异常。未捕获的异常会导致程序终止。

您是如何创建阵列的?导致这种情况的最常见错误是执行以下操作:

[mutableArray copy]

即使您正在复制的东西是可变的,该副本也是不可变的。在这个随机选择的示例中,使用mutableCopy insted。

在某些地方,您已将NSMutableArray1设置为NSArray的实例,而不是NSMutableArray,或者您将NSMutableArray1声明为NSArray与NSMutabableArray类型的属性。

此外,您应该遵循Cocoa/Objective-C命名约定。也就是说,类名以大写字母开头;变量采用myArray1的形式(或者更具描述性的形式)。





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签