简体   繁体   English

Objective-C NSMutableArray计数导致EXC_BAD_ACCESS

[英]Objective-C NSMutableArray Count Causes EXC_BAD_ACCESS

I've been stuck on this for days and each time I come back to it I keep making my code more and more confusing to myself, lol. 我已经坚持了好几天,每次回到它时,我都会不断使自己的代码变得越来越混乱,大声笑。 Here's what I'm trying to do. 这就是我想要做的。 I have table list of charges, I tap on one and brings up a model view with charge details. 我有收费表,点击一下,会弹出一个带有收费明细的模型视图。 Now when the model is presented a object is created to fetch a XML list of users and parses it and returns a NSMutableArray via a custom delegate. 现在,当呈现模型时,将创建一个对象来获取用户的XML列表并进行解析,并通过自定义委托返回NSMutableArray。 I then have a button that presents a picker popover, when the popover view is called the user array is used in an initWithArray call to the popover view. 然后,我有一个按钮,用于显示选取器弹出窗口,当调用弹出窗口视图时,在对弹出窗口视图的initWithArray调用中使用了用户数组。 I know the data in the array is right, but when [pickerUsers count] is called I get an EXC_BAD_ACCESS. 我知道数组中的数据正确,但是当调用[pickerUsers count]时,我得到了EXC_BAD_ACCESS。 I assume it's a memory/ownership issue but nothing seems to help. 我认为这是内存/所有权问题,但似乎无济于事。 Any help would be appreciated. 任何帮助,将不胜感激。

Relevant code snippets: 相关代码段:

Charge Popover (Charge details model view): 收费弹出窗口(收费详细信息模型视图):

@interface ChargePopoverViewController .....
NSMutableArray *pickerUserList;
@property (nonatomic, retain) NSMutableArray *pickerUserList;

@implementation ChargePopoverViewController
@synthesize whoOwesPickerButton, pickerUserList;
- (void)viewDidLoad {   
    JEHWebAPIPickerUsers *fetcher = [[JEHWebAPIPickerUsers alloc] init];
    fetcher.delegate = self;
    [fetcher fetchUsers];
}
-(void) JEHWebAPIFetchedUsers:(NSMutableArray *)theData {
    [pickerUserList release];
    pickerUserList = theData;
}
- (void) pickWhoPaid: (id) sender {
    UserPickerViewController* content = [[UserPickerViewController alloc] initWithArray:pickerUserList];
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:content];
    [popover presentPopoverFromRect:whoPaidPickerButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    content.delegate = self;    
}

User Picker View Controller 用户选择器视图控制器

@interface UserPickerViewController .....
NSMutableArray *pickerUsers;
@property(nonatomic, retain) NSMutableArray *pickerUsers;

@implementation UserPickerViewController
@synthesize pickerUsers;
-(UserPickerViewController*) initWithArray:(NSMutableArray *)theUsers {
    self = [super init];

    if ( self ) {
        self.pickerUsers = theUsers;
    }

    return self;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    // Dies Here EXC_BAD_ACCESS, but NSLog(@"The content of array is%@",pickerUsers); shows correct array data 
    return [pickerUsers count];
}

I can provide additional code if it might help. 如果有帮助,我可以提供其他代码。 Thanks in advance. 提前致谢。

You declare the ivar holding the array as this... 您将保存数组的ivar声明为此...

@property (nonatomic, retain) NSMutableArray *pickerUserList;

But then you have a method implemented like this: 但是然后您实现了这样的方法:

-(void) JEHWebAPIFetchedUsers:(NSMutableArray *)theData {
    [pickerUserList release];
    pickerUserList = theData;
}

You aren't retaining theData and you aren't calling the synthesized setter. 您没有保留theData ,也没有调用综合设置器。 If you did Build and Analyze , it should catch this problem and tell you about it. 如果您进行过Build and Analyze ,它应该可以解决此问题并告诉您。 If not, file a bug. 如果没有,请提交错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM