iPhone-在NSMutableArray上使用addObject崩溃
我这里有个问题。也许我真的很累 我有一门课:iPhone-在NSMutableArray上使用addObject崩溃,iphone,crash,nsmutablearray,add,Iphone,Crash,Nsmutablearray,Add,我这里有个问题。也许我真的很累 我有一门课: @interface THECLASS : UIViewController <UITableViewDelegate> { NSMutableArray* param; } @property(nonatomic, retain) NSMutableArray* param; 这会使用以下日志使应用程序崩溃: 2011-02-04 01:31:02.548 Learning Project[3895:207] -[__NSA
@interface THECLASS : UIViewController <UITableViewDelegate> {
NSMutableArray* param;
}
@property(nonatomic, retain) NSMutableArray* param;
这会使用以下日志使应用程序崩溃:
2011-02-04 01:31:02.548 Learning Project[3895:207] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x630c960
2011-02-04 01:31:02.549 Learning Project[3895:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x630c960'
*** Call stack at first throw:
(
0 CoreFoundation 0x00fc4be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x011195c2 objc_exception_throw + 47
2 CoreFoundation 0x00fc66fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00f36366 ___forwarding___ + 966
4 CoreFoundation 0x00f35f22 _CF_forwarding_prep_0 + 50
5 Learning Project 0x00019463 -[ChoixJoursDisponibiliteController changedSelectorValue:isOn:] + 644
6 Learning Project 0x000190db -[ChoixJoursDisponibiliteController clickChangedSelectorValue:] + 307
7 UIKit 0x002f6a6e -[UIApplication sendAction:to:from:forEvent:] + 119
8 UIKit 0x003851b5 -[UIControl sendAction:to:forEvent:] + 67
9 UIKit 0x00387647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
10 UIKit 0x004c9c6d -[UISwitch _onAnimationDidStop:finished:context:] + 201
11 UIKit 0x00327665 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 294
12 UIKit 0x003274f7 -[UIViewAnimationState animationDidStop:finished:] + 77
13 QuartzCore 0x01eab6cb _ZL23run_animation_callbacksdPv + 278
14 QuartzCore 0x01eab589 _ZN2CAL14timer_callbackEP16__CFRunLoopTimerPv + 157
15 CoreFoundation 0x00fa5fe3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
16 CoreFoundation 0x00fa7594 __CFRunLoopDoTimer + 1220
17 CoreFoundation 0x00f03cc9 __CFRunLoopRun + 1817
18 CoreFoundation 0x00f03240 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x00f03161 CFRunLoopRunInMode + 97
20 GraphicsServices 0x018f9268 GSEventRunModal + 217
21 GraphicsServices 0x018f932d GSEventRun + 115
22 UIKit 0x0030542e UIApplicationMain + 1160
23 Learning Project 0x00002580 main + 102
24 Learning Project 0x00002511 start + 53
25 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'
参数从调用者视图中由以下内容填充:
NSMutableArray* filledArray = [NSMutableArray arrayWithObjects:@"SOME TEXT", nil]
nextController.param = filledArray;
NSLog(@“%@”,self.param)代码>在崩溃之前给出:
2011-02-04 02:01:17.205 Learning Project[4223:207] (
"JOURDISPO_MARDI"
)
为什么会崩溃?
数组在那里,填充而不是零。。。它似乎定义得很清楚。。。addObject是一个NSMutableArray方法。。。我不明白
我没有在这里显示它,但是removeObject有相同的日志。由于某种原因,您正在对NSArray
的实例调用addObject:
方法,如堆栈跟踪所示。您的param
对象的合成getter可能返回一个NSArray
?不要在方法中使用getter:
- (void) changedSelectorValue:(NSIndexPath*)indexPath isOn:(BOOL)isOn {
[param addObject:@"eeeee"];
}
最终,错误是正确的。您正在对不可变对象调用一个变异方法。奇怪的是,在我的测试中,这不会导致编译器错误或警告:
NSMutableArray *array = [[NSArray alloc] init];
Edit:不管您是否相信,param
ivar被设置为NSArray的实例。覆盖设置程序:
- (void)setParam:(NSMutableArray *)p {
if (param != p) {
[param release];
param = [p retain];
}
}
然后在该方法上添加一个断点,并检查NSArray何时传入。您的init方法是什么样子的?应该是这样的
- (id)init
{
self = [super init];
self.param = [NSMutableArray array];
return self;
}
您的属性定义为retain
,您说param
是“调用者视图中的填充数组”。这可能是你的问题
例如,如果您执行以下操作:
NSArray * filledArray = [NSArray arrayWithObjects:..., nil];
theClassInstance.param = filledArray;
您将保留一个不可变数组
编辑:要调试param
的设置,可以在.m中执行以下操作:
@dynamic param;
- (NSMutableArray*)param { return param; }
- (void)setParam:(NSMutableArray*)newArray {
NSLog(@"setting new param from class: %@",
NSStringFromClass([newArray class]));
[newArray retain];
[param release];
param = newArray;
}
答案隐藏在对“已接受”答案的评论中。我从谷歌来到这里,它解决了我的问题,所以。。。更清楚地说,根据@e.James的评论:
当您实现“NSCopying”协议时
“copyWithZone”,您不得在内部可变数组上使用“copy”-此
不复制数组(而是创建不可变的副本)
e、 g.根据我自己的代码:
// Class: MyClass
@property(nonatomic, retain) NSMutableArray* mutArray;
-(id)copyWithZone:(NSZone *)zone
{
MyClass* other = [[MyClass alloc] init];
// other.mutArray = [self.mutArray copy]; // WRONG!
other.mutArray = [self.mutArray mutableCopy]; // CORRECT!
return other;
}
经过努力,我找到了解决问题的办法。我的问题是
我有一个可变字典和数组
@property(strong,nonatomic)NSMutableDictionary *dictInfo;
@property(retain,nonatomic) NSMutableArray *userAccountsList;
我正在向userAccountsList数组添加服务器数组列表,如下所示
self.dictInfo = [jsonObject valueForKey:@"customerAccountList"];
userAccountsList = [NSMutableArray arrayWithArray:array];
我已获取另一个数组,并将服务器数组列表添加到该数组中
array = [self.dictInfo valueForKey:@"banker"];
最后向UserAccountsListaray添加数组,如下所示
self.dictInfo = [jsonObject valueForKey:@"customerAccountList"];
userAccountsList = [NSMutableArray arrayWithArray:array];
在这里向数组添加额外的对象
[userAccountsList addObject:@"Other Account"];//I was getting error here.
[tableViewObj reloadData];
希望它能帮助别人。如果有帮助,请投赞成票。您如何初始化param
属性?@Dave DeLong:从调用方视图看,这是一个填充数组。没关系,没问题。它是填充的,在其他地方,我使用它进行一些显示。它在外部由[NSMutableArray arrayWithObjects:@“some TEXT”,nil]填充。阅读注释,由调用方视图填充。这个数组没有问题,它在控制器的其他地方填充和使用都没有问题。即使调试器也会显示正确的数组。为了正确执行操作,您应该始终确保[super init]
在执行进一步初始化之前不会返回nil。我在上面的第一行中使用:如果((self=[super init])==nil){return nil;}
,请确保检查[super init]是否返回nil。另外,在iPhone上(尽管在任何情况下,确实如此),最好避免为IVAR自动删除对象,这只是浪费计算时间。@e.James:不,它是外部的NSMutableArray,而不是NSArray。我认为它必须是[Filled MutableArray mutableCopy]
,否则你只能再次得到NSArray
。但不确定这是否是问题所在。可以添加NSLog(@“%@”,param)代码>在崩溃之前,在方法中查看它是什么类型的对象?实际上,请划掉NSLog
。很明显,param
在代码的某个地方被设置为不可变的NSArray
。为param
属性创建一个自定义setter,然后在崩溃发生之前记录其行为,这可能是有益的。使用该选项,您应该能够确定param
何时变为不可修改的自定义设置程序无法解决您的问题。我建议将其作为一种调试方法。日志输出应该告诉您从何处获取不可变数组。