【问题标题】:NSXMLParser foundCharacters Thread 1: EXC_BAD_ACCESS (code=2, address=0x0)NSXMLParser foundCharacters 线程 1: EXC_BAD_ACCESS (code=2, address=0x0)
【发布时间】:2014-09-19 03:50:43
【问题描述】:

我正在尝试将 rss 阅读器实现到容器内的表中。我在另一个项目中有解析器的代码,在特定项目中它工作正常,没有任何错误。我复制了每个文件,4 个文件与它们在另一个项目中的完全相同,并且我还将视图控制器复制到了我的情节提要中,因此它们完全相同。但是当我运行代码时出现错误!

here is a pic with my error.

我不知道为什么会这样。在另一个只有两个视图控制器的项目中,它工作正常。但是当我尝试将每个文件原样复制到我的另一个项目中时,我得到了这个错误。

这是我的 .m 文件

#import "APPMasterViewController.h"
#import "APPDetailViewController.h"

@interface APPMasterViewController () {
    NSXMLParser *parser;
    NSMutableArray *feeds;
    NSMutableDictionary *item;
    NSMutableString *title;
    NSMutableString *link;
    NSString *element;
}
@end

@implementation APPMasterViewController

(void)awakeFromNib {
    [super awakeFromNib];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    feeds = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:@"http://radioevros.gr/feed/"];
    parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table View

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return feeds.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath        *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"      forIndexPath:indexPath];
    cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
    return cell;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:   (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    element = elementName;
    if ([element isEqualToString:@"item"]) {
        item    = [[NSMutableDictionary alloc] init];
        title   = [[NSMutableString alloc] init];
        link    = [[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"item"]) {
        [item setObject:title forKey:@"title"];
        [item setObject:link forKey:@"link"];
        [feeds addObject:[item copy]];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if ([element isEqualToString:@"title"]) {
        [title appendString:string];
    } else if ([element isEqualToString:@"link"]) {
        [link appendString:string];
    }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
    [self.tableView reloadData];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
        [[segue destinationViewController] setUrl:string];
    }
}

-(void) viewDidUnload{
    [element release];
    element = nil;
    [super viewDidUnload];
}

- (void)dealloc {
    [element release];
    [super dealloc];
}

@end

【问题讨论】:

  • 请将错误放入问题本身。 SO 尽量不依赖外部资源来保持更新并为未来的读者保留他们的内容。

标签: parsing ios7 xcode5 nsxmlparser


【解决方案1】:

我不能肯定地说,因为我没有尝试运行您的代码,但您可能想尝试使用 element = [elementName copy]; 存储元素,看看是否能消除异常。

我的猜测是,elementName 在您退出该方法后会被释放,因为它只是一个局部参数变量。

附带说明,您似乎正在执行手动内存管理。我强烈建议您将项目转换为使用 ARC,因为它不仅可以简化内存管理,而且 Apple 一直在推荐使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2013-10-14
    • 2020-01-10
    • 2014-01-01
    • 1970-01-01
    相关资源
    最近更新 更多