解析RSS Feed时发送到实例的无法识别的选择器

时间:2018-03-12 05:33:44

标签: ios objective-c rss

相关Feed是https://fritchcoc.podbean.com/feed/

我的代码中有解析,但每次运行时,都会收到以下错误消息。我试图添加异常断点,但它没有向我显示引起所有骚动的代码行。我花了两个小时就完成了这个问题并没有运气。我在命令中将每个NSString设置为nil,以防其中一个valueForChild项出现错误,但即使是所有nils,它们都有问题。

  

由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [RSSEntry initWithBlogTitle:articleTitle:articleUrl:articleDate:articleImage:contentEncoded:]:无法识别的选择器发送到实例

- (void)viewDidLoad {
    [super viewDidLoad];

    self.allEntries = [NSMutableArray array];
    self.queue = [[[NSOperationQueue alloc] init] autorelease];
    self.feeds = [NSArray arrayWithObjects:@"https://fritchcoc.podbean.com/feed/",
                  nil];   


    [self refresh];
}

- (void)refresh {
    for (NSString *feed in _feeds) {
        NSURL *url = [NSURL URLWithString:feed];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setDelegate:self];
        [_queue addOperation:request];
    }

}

- (void)parseFeed:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
    if ([rootElement.name compare:@"rss"] == NSOrderedSame) {
        [self parseRss:rootElement entries:entries];
    } else if ([rootElement.name compare:@"feed"] == NSOrderedSame) {                       
        [self parseAtom:rootElement entries:entries];
    } else {
        NSLog(@"Unsupported root element: %@", rootElement.name);
    }    
}

- (void)requestFinished:(ASIHTTPRequest *)request {
    [_queue addOperationWithBlock:^{
        NSError *error;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[request responseData] 
                                                               options:0 error:&error];
        if (doc == nil) { 
            NSLog(@"Failed to parse %@", request.url);
        } else {
            NSMutableArray *entries = [NSMutableArray array];
            [self parseFeed:doc.rootElement entries:entries];                

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                //int newCounter = 0;

                for (RSSEntry *entry in entries) {
                  //  newCounter++;
                    int insertIdx = [_allEntries indexForInsertingObject:entry sortedUsingBlock:^(id a, id b) {
                        RSSEntry *entry1 = (RSSEntry *) a;
                        RSSEntry *entry2 = (RSSEntry *) b;
                        return [entry1.articleDate compare:entry2.articleDate];
                    }];

                    [_allEntries insertObject:entry atIndex:insertIdx];
                    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:0]]
                                          withRowAnimation:nil];
                   /* if (newCounter > 999) {
                        break;
                    }*/
                }                            
            }];
        }        
    }];
}

- (void)requestFailed:(ASIHTTPRequest *)request {
    NSError *error = [request error];
    NSLog(@"Error: %@", error);
    [self refresh];
}

- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {
    NSLog(@"Go");
    NSArray *channels = [rootElement elementsForName:@"channel"];
    for (GDataXMLElement *channel in channels) {            
        NSString *blogTitle = [channel valueForChild:@"title"];                    

        NSArray *items = [channel elementsForName:@"item"];
        for (GDataXMLElement *item in items) {
            NSString *articleTitle = [item valueForChild:@"title"];
            NSString *articleDateString = [item valueForChild:@"pubDate"];
            NSString *theCategory = [item valueForChild:@"category"];
            NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
            NSString *articleUrl = [[[[item elementsForName: @"enclosure"] lastObject] attributeForName: @"url"] stringValue];
            NSString *picture = [[[[item elementsForName: @"media:content"] lastObject] attributeForName: @"href"] stringValue];
            RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle 
                                                      articleTitle:articleTitle 
                                                        articleUrl:articleUrl 
                                                       articleDate:articleDate
                                                        articleImage:picture
                                                        contentEncoded:nil
                                                        category:theCategory] autorelease];
            if ([theCategory isEqualToString:@"Sermon"]) {
                [entries addObject:entry];
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在您的代码中,您有这个电话:

    RSSEntry *entry = [[[RSSEntry alloc] initWithBlogTitle:blogTitle 
                                              articleTitle:articleTitle 
                                                articleUrl:articleUrl 
                                               articleDate:articleDate
                                                articleImage:picture
                                                contentEncoded:nil
                                                category:theCategory] autorelease];

它有一个“category”最后一个参数,但是你的错误信息没有:

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException' 的,   原因:' - [RSSEntry initWithBlogTitle:articleTitle:articleUrl:articleDate:articleImage:contentEncoded:]:   无法识别的选择器发送到实例

是否有可能使RSSEntry的新版本具有“类别”,但没有正确地重新编译某些解析代码,并且该代码仍在尝试调用旧方法(没有“类别”) ? 如果是这样,只需做一个产品 - “清理” - “构建”,并注意编译错误警告。

我注意到你仍然没有使用ARC(称为“autorelease”),这表明这是一个非常古老的代码:)