crashes my app [NSMutableArray1 removeAllObjects] iphone sdk

旧街凉风 提交于 2019-12-04 19:51:27
mutable1 = [[NSMutableArray alloc] init];
mutable1 = [prefs1 objectForKey:@"favorites"];

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.

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

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

The error messages indicate that you're sending the message to an immutable array, which raises an exception. Uncaught exceptions lead to program termination.

How are you creating the array? The most common error that can lead to this is doing something like:

[mutableArray copy]

Even if the thing you're copying is mutable, the copy will be immutable. In that randomly chosen example, use mutableCopy insted.

Somewhere you have set NSMutableArray1 to an instance of NSArray, not NSMutableArray or you declared NSMutableArray1 as a property of type NSArray vs. NSMutableArray.

Also, you should follow Cocoa / Objective-C naming conventions. Namely, class names start with upper case; variables take the form myArray1 (or something more descriptive, preferably).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!