user717452
user717452

Reputation: 111

Unrecognized Selector sent to Instance when parsing RSS Feed

The feed in question is https://fritchcoc.podbean.com/feed/

I have this in my code to parse, but every time I run it, I get an error message of the following. I have tried to add exception breakpoints, but it is not showing me the line of code causing all the commotion. I have spent two hours on this with no luck at all figuring out the issue. I set every NSString to nil within the commands, just in case one of the item valueForChild was coming up with errors, but even with all nils, they had issues.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RSSEntry initWithBlogTitle:articleTitle:articleUrl:articleDate:articleImage:contentEncoded:]: unrecognized selector sent to instance

- (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];
            }
        }
    }
}

Upvotes: 0

Views: 63

Answers (1)

battlmonstr
battlmonstr

Reputation: 6300

In your code you have this call:

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

It has a "category" last parameter, but your error message doesn't have it:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RSSEntry initWithBlogTitle:articleTitle:articleUrl:articleDate:articleImage:contentEncoded:]: unrecognized selector sent to instance

Is it possible that you have made a new version of RSSEntry that has "category", but didn't recompile properly some of your parsing code, and that code is still trying to call an old method (without "category")? If so, just do a Product - "Clean" - "Build", and pay attention to compilation errors an warnings.

I've noticed that you still are not using ARC (calling "autorelease"), which indicates to me that it's a really old code :)

Upvotes: 1

Related Questions