2

iOS初心者はこちら ニュース Web サイト用の RSS リーダーを作成しようとしています。RSS パーサーは正常に動作していますが、ARC を有効にしてコンパイルすると、例外 EXC_BAD_ACCESS (code=2 address=0xc) が発生します。

パーサーを呼び出す方法は次のとおりです:(ビューコントローラーで)

NSMutableArray* articleListMainPage;

- (void)viewDidLoad
{
    NSURL *mainFeed = [[NSURL alloc]initWithString:@"http://my.domain.com/rss.xml"];
    NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithContentsOfURL:mainFeed];
    NewsParser *myparser = [[NewsParser alloc] initNewsParser];
    [nsXmlParser setDelegate:myparser];

    if ([nsXmlParser parse]) {
        NSLog(@"Parsed article count : %i", [myparser.articles count]);
        articleListMainPage = [myparser.articles copy];
    } else {
        NSLog(@"Error parsing document!");
    }
    nsXmlParser = nil;
    mainFeed = nil;
    myparser = nil; // This line throws the exception.

    [super viewDidLoad];
}

パーサー自体は次のとおりです。

// NewsParser.m
#import "NewsParser.h"
#import "RSSEntry.h"

@implementation NewsParser

@synthesize article, articles, currValue;

- (NewsParser *) initNewsParser
{
    if (self = [super init]) return self; else return nil;
}

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

    if ([elementName isEqualToString:@"channel"]) {
        self.articles = [[NSMutableArray alloc] init];
    }
    if ([elementName isEqualToString:@"item"]) {
        self.article = [[ArticleEntry alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (!currValue) {
        self.currValue = [[NSMutableString alloc] initWithString:string];
    } else {
        [self.currValue appendString:string];
    }
}

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

    if ([elementName isEqualToString:@"channel"]) {
        return;
    }

    if ([elementName isEqualToString:@"item"]) {
        [self.articles addObject:self.article];
        self.article = nil;
    } else {
        if ([[NSArray arrayWithObjects:@"title",@"link",@"description",nil] containsObject:elementName])
             [self.article setValue:self.currValue forKey:elementName];
    }
    self.currValue = nil;
}

@end

//NewsParser.h

#import <Foundation/Foundation.h>
#import "RSSEntry.h"

@interface NewsParser : NSXMLParser <NSXMLParserDelegate> {
    NSMutableString *currValue;
    NSMutableArray *articles;
    ArticleEntry *article;
}
- (NewsParser *) initNewsParser;
@property (nonatomic,retain) ArticleEntry *article;
@property (nonatomic,retain) NSMutableArray *articles;
@property (nonatomic,retain) NSMutableString *currValue;
@end

.. そして、RSS エントリを保存するために使用するクラス。

//RSSEntry.h
#import <Foundation/Foundation.h>

@interface ArticleEntry : NSObject {
    NSString *title;
    NSString *link;
    NSString *description;
}

@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *link;
@property (nonatomic, retain) NSString *description;

- (id)initNewArticle:(NSString*)_title url:(NSString*)_link description:(NSString*)_desc;

@end

//RSSEntry.m

#import "RSSEntry.h"

@implementation ArticleEntry
@synthesize title,link,description;
- (id)initNewArticle:(NSString *)_title url:(NSString *)_link description:(NSString *)_desc
{
    if ((self = [super init])) {
        title = [_title copy];
        link = [_link copy];
        description = [_desc copy];
        return self;
    }
    else return nil;
}
@end

私が言ったように、ARC がなくても問題なく動作しますが、コードですべきでないことを行っているかどうかを知りたいです (たとえば、すべきでないことを保持したり、何かを適切にリリースしたり、何かを過剰にリリースしたりしていません)。

御時間ありがとうございます..

4

1 に答える 1