ios - EXC_BAD_ACCESS 与 NSXMLParser 和 ARC

标签 ios automatic-ref-counting exc-bad-access nsxmlparser

iOS 开发的初学者,我将尝试实现一个通过 XML 文件显示 RSS feed 的应用程序。

在我的 UITableView 类的 viewDidLoad 中,我使用 UIActivityIndi​​cator 来等待数据加载。

但是,当应用程序返回主线程时,我在 parseXMLStart 函数末尾有一个 EXC_BAC_ACCESS 代码 2。我不明白为什么...

这里是错误消息:

Thread 6 : 0-[NSXMLParser dealloc]
Message  : EXC_BAC_ACCESS (code=2, address=0xc)
Line     : 0xbb0840:  movl   (%eax,%ecx), %ecx

我不知道我的错误是什么以及在哪里。我该如何修复它?

<小时/>

这是我的代码:

=> Class Type :: UITableViewController    

>> Header

@interface DataListViewController : UITableViewController {
    UIActivityIndicatorView *activityView;
    NSMutableArray *dataFromXML;
}

- (void)parseXMLStart;
- (void)parseXMLDone;

@end

>> Main

@implementation DataListViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"View 1";

    activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityView.center = self.view.center;

    [self performSelectorInBackground:@selector(parseXMLStart) withObject:nil];

    [activityView startAnimating];
    [activityView setHidesWhenStopped:YES];

    [self.view addSubview:activityView];
}

#pragma mark - UIActivityIndicator Methods

- (void)parseXMLStart
{
    // To Show the animation
    sleep(1);

    dataFromXML = [[NSMutableArray alloc] init];

    // COMMENT TO TEST /*

    [dataFromXML addObject:@"Element 1"];
    [dataFromXML addObject:@"Element 2"];
    [dataFromXML addObject:@"Element 3"];

    // */ COMMENT TO TEST

    // ------------------------------------------------------------------------------------------------------------------------------------

    // UNCOMMENT TO TEST
    /*

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"xml"];
    NSURL *url = [[NSURL alloc] initWithString:[[NSString stringWithFormat:@"file://%@",filePath] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];

     XML2ObjectParser *parserWords = [[XML2ObjectParser alloc] parseXMLAtURL:url toObject:@"Word" parseError:nil];

    NSLog(@">> parserWords Items Count :: %i", parserWords.items.count);

    for (int i = 0; i < [parserWords.items count]-1; i++) {
        Word *aWord = [[Word alloc] init];
        aWord = (Word *)[[parserWords items] objectAtIndex:i];
        [dataFromXML addObject:aWord];
    }

    NSLog(@">> dataFromXML Count :: %i", dataFromXML.count);

    */
    // UNCOMMENT TO TEST

    // --------------------------------------------------------------------------------------------------------------------------------------------

    // EXC_BAD_ACCESS (code=2, address=0xc)
    // Thread 6 : 0-[NSXMLParser dealloc]
    // 0xbad840:  movl   (%eax,%ecx), %ecx

    // --------------------------------------------------------------------------------------------------------------------------------------------

    [self performSelectorOnMainThread:@selector(parseXMLDone) withObject:nil waitUntilDone:YES];
}

- (void)parseXMLDone
{
    [activityView stopAnimating];
    [self.tableView reloadData];
}

# pragma mark - Table View Method

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataFromXML count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    cell.textLabel.text = [dataFromXML objectAtIndex:[indexPath row]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DataListDetailViewController *_dataListDetailViewController = [[DataListDetailViewController alloc] init];

    _dataListDetailViewController.title = [dataFromXML objectAtIndex:[indexPath row]];

    [self.navigationController pushViewController:_dataListDetailViewController animated:YES];
}

@end

最佳答案

在我看来,您的选择器输入不正确。你有@selector(parsingXMLDone),它应该是@selector(parseXMLDone)

关于ios - EXC_BAD_ACCESS 与 NSXMLParser 和 ARC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13955847/

相关文章:

ios - UIDevice setOrientation :animated: causing EXC_BAD_ACCESS

ios - 为什么 PhoneGap 总是显示默认的闪屏图像?

iphone - 使用 ARC(强/弱)避免循环保留,学习一些基础知识

cocoa - 非ARC下如何清零弱引用?

iOS:lldb EXC_BAD_ACCESS 自定义单元格

ios - SpriteKit SKEmitterNode targetNode 崩溃 EXC_BAD_ACCESS

ios - OpenGL ES 纹理不渲染

ios - Objective-c 中的 class_replaceMethod 不适用于 Swift 类

ios - XCTest 的@testable 幕后发生了什么?

cocoa - CGContextRelease - 不被拥有的对象的引用计数的错误减少