崩溃我的应用程序[NSMutableArray1 removeAllObjects] iphone sdk [英] crashes my app [NSMutableArray1 removeAllObjects] iphone sdk

查看:156
本文介绍了崩溃我的应用程序[NSMutableArray1 removeAllObjects] iphone sdk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码检查我的NSMutableArray中是否存在任何对象
如果是,我将它们全部删除但它崩溃了虽然有对象为什么?

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];
    }

CONSOLE OUTPUT

CONSOLE OUTPUT


2010-10-18 03:42:13.166
app1 [33398:207] *由于未捕获的异常终止app

'NSInternalInconsistencyException',
原因:' - [__ NSCFArray
removeObjectAtIndex:]:mutating方法
发送到不可变对象'
*
第一次调用堆栈:(0 CoreFoundation

0x02e55b99 exceptionPreprocess + 185
1 libobjc.A.dylib

0x02fa540e objc_exception_throw + 47
2 CoreFoundation

0x02e0e238 + [NSException
加注:格式:参数:] + 136 3

CoreFoundation

0x02e0e1aa + [NSException
加注:格式:] + 58 4 $
CoreFoundation

0x02e4d3c1 - [__ NSCFArray
removeObjectAtIndex:] + 193 5

CoreFoundation

0x02dfe973 - [NSMutableArray
removeAllObjects] + 83 6

poemsoflove

0 x0004dc8d - [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 CoreFoundati on

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)在抛出
'NSException实例后终止调用
'节目收到信号:
SIGABRT。

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的家伙!

Guys there is no NSArray!

我保存到这样的NSUSerdefaults:

I save to NSUSerdefaults like this:

if([mutable1 count] == 0)
{
[mutable1 addObject:final1];
}

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

and i get the objects! then when it runs the removeallobjects it crashes!

推荐答案

mutable1 = [[NSMutableArray alloc] init];
mutable1 = [prefs1 objectForKey:@"favorites"];

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

Even though you've declared mutable1 to be an NSMutableArray, you are reassigning it to the object returned by your NSUserDefaults object. This object is apparently an NSArray rather than an NSMutableArray, hence the crash.

你可以通过这样的方式加载你的NSMutableArray和preferences数组:

You can load your NSMutableArray with the preferences array by doing something like this:

mutable1 = [[NSMutableArray alloc] init];
[mutable1 addObjectsFromArray:[prefs1 objectForKey:@"favorites"]];

这篇关于崩溃我的应用程序[NSMutableArray1 removeAllObjects] iphone sdk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆