iPhone - 使用NSMutableArray上的addObject进行崩溃 [英] iPhone - Crash using addObject on a NSMutableArray

查看:1059
本文介绍了iPhone - 使用NSMutableArray上的addObject进行崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有问题。或者我真的很累...



我有一个类:

  @interface THECLASS:UIViewController< UITableViewDelegate> {
NSMutableArray * param;
}

@property(nonatomic,retain)NSMutableArray * param;

在这个类中,我有一个方法,当用户点击一个UISwitch内部的tableViewCell我建立的params到一个IBAction方法,不是shwon这里):

  @synthesize param; 

- (void)changedSelectorValue:(NSIndexPath *)indexPath isOn:(BOOL)isOn {
[self.param addObject:@eeeee];
}

这会使应用程序崩溃,并显示以下日志:

  2011-02-04 01:31:02.548学习项目[3895:207]  -  [__ NSArrayI addObject:]:无法识别的选择器发送到实例0x630c960 
2011-02-04 01:31:02.549学习项目[3895:207] ***由于未捕获异常NSInvalidArgumentException终止应用程序,原因:' - [__ NSArrayI addObject:]:无法识别的选择器发送到实例0x630c960'
***首次调用堆栈:

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学习项目0x00019463 - [ChoixJoursDisponibiliteController changedSelectorValue:ISON:] + 644
6学习项目0x000190db - [ChoixJoursDisponibiliteController clickChangedSelectorValue:] + 307
7 UIKit的0x002f6a6e - [UIApplication的sendAction::从:forEvent:] + 119
8 UIKit的0x003851b5 - [UIControl sendAction:到:forEvent:] + 67
9 UIKit的0x00387647 - [UIControl(内部)_sendActionsForEvents:withEvent:方法] + 527
10的UIKit 0x004c9c6d - [UISwitch _onAnimationDidStop:完成:上下文:] + 201
11的UIKit 0x00327665 - [ UIViewAnimationState sendDelegateAnimationDidStop:完成:] + 294
12的UIKit 0x003274f7 - [UIViewAnimationState animationDidStop:完成:] + 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学习项目0x00002580主+ 102
24学习项目0x00002511启动+ 53
25 ??? 0x00000001 0x0 + 1

在抛出NSException实例后调用终止

pre $ p> NSMutableArray * filledArray = [NSMutableArray arrayWithObjects:@SOME TEXT ,nil]
nextController.param = filledArray;

NSLog(@%@,self.param) code>刚刚之前的崩溃给出:

  2011-02-04 02:01:17.205学习项目[4223: 207](
JOURDISPO_MARDI

为什么会崩溃?
数组是有,填充和不是nil ...似乎很好定义... addObject是一个NSMutableArray方法...我不明白



解决方案

您的属性定义为 retain ,并且你说 param 是从调用者视图填充的数组。这可能是您的问题。



例如,如果您执行以下操作:

  NSArray * filledArray = [NSArray arrayWithObjects:...,nil]; 
theClassInstance.param = filledArray;

您将保留一个不可变数组。



编辑 要调试 param 的设置,您可以在.m中执行以下操作:

  @dynamic param; 
- (NSMutableArray *)param {return param; }
- (void)setParam:(NSMutableArray *)newArray {
NSLog(@从类设置新参数:%@,
NSStringFromClass([newArray class]))
[newArray retain];
[param release];
param = newArray;
}


I have a problem here. Or Maybe I'm really tired...

I have a class :

@interface THECLASS : UIViewController <UITableViewDelegate> {
    NSMutableArray* param;
}

@property(nonatomic, retain) NSMutableArray* param;

Inside that class I have a method that is called when the user clicks on a UISwitch inside a tableViewCell (I build the params into a IBAction method that is not shwon here) :

@synthesize param;

- (void) changedSelectorValue:(NSIndexPath*)indexPath isOn:(BOOL)isOn {
    [self.param addObject:@"eeeee"];
}

This crashes the app with the following log :

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'

param is filled from a caller view by something like :

NSMutableArray* filledArray = [NSMutableArray arrayWithObjects:@"SOME TEXT", nil] 
nextController.param = filledArray;

NSLog(@"%@", self.param); just before the crash gives :

2011-02-04 02:01:17.205 Learning Project[4223:207] (
    "JOURDISPO_MARDI"
)

Why does this crash ? The array is there, filled and not nil... It seems well defined... addObject is a NSMutableArray method... I don't understand

I don't show it here but there is the same log with removeObject.

解决方案

Your property is defined as retain, and you say that param is "a filled array from the caller view". This could be your problem.

For example, if you do the following:

NSArray * filledArray = [NSArray arrayWithObjects:..., nil];
theClassInstance.param = filledArray;

You will have retained a non-mutable array.

Edit: To debug the setting of param, you could do the following in your .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;
}

这篇关于iPhone - 使用NSMutableArray上的addObject进行崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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