xmlxcodearraysparsingdouble-submit-problem

Xcode XML File Parsing From a Web Site


I have an issue with my code but can't understand where the problem is.

I am parsing the following XML file

  <item>
            <title>Title</title>
            <link>http://news.israelinfo.ru/economy/45276</link>
            <category>economy</category>
            <pubDate>Tue, 16 Apr 2013 00:00:00 +0300</pubDate>
            <description><![CDATA[
            <img src="http://news.israelinfo.ru/images/45276.jpg"
            width="180" height="124" align="left">describe article]]></description>
     </item>

I am getting in array count of my news (it's always 20), but when I try to put a description in my custom cell my app is crashing :(

Here is the error:

2013-04-17 23:52:05.258 IsraelNews[20379:c07] -[lastNews isEqualToString:]: unrecognized selector sent to instance 0x715b460 2013-04-17 23:52:05.259 IsraelNews[20379:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[lastNews isEqualToString:]: unrecognized selector sent to instance 0x715b460'

cell.titleLabel.text = [news objectAtIndex:5];

If I put static text @"Test", then everything is OK.

What I am doing wrong? If you need any other information, please let me know.

Thanks

My XML parser is here:

-(id)init {

    if (self=[super init]) {

        news = [[NSMutableArray alloc]init];
        dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzzz"];

    }
    return self;
}

-(NSArray*)fetchNewsWithError:(NSError*)outError {
    BOOL success;
    NSURL *xmlUrl = [NSURL URLWithString:@"http://israelinfo.ru/xml/news.xml"];
    NSURLRequest *request = [NSURLRequest requestWithURL:xmlUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
    NSURLResponse *response = nil;

    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&outError];

    if (!data) return nil;

    [news removeAllObjects];
    NSXMLParser *parser;
    parser = [[NSXMLParser alloc]initWithData:data];
    [parser setDelegate:self];
    success = [parser parse];
    if (!success) {

        NSLog(@"error!");
        return nil;

    }

    NSArray *output = [news copy];
    return output;

}


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

    if ([elementName isEqual:@"item"]) {
        currentFields = [[NSMutableDictionary alloc]init];
    } else if ([elementName isEqual:@"title"]) {

        [currentFields setObject:@"title" forKey:@"title"];

    }
}


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


    if ([elementName isEqual:@"item"]) {

        lastNews *currentTitle = [[lastNews alloc]init];
        [currentTitle setTitle:[currentFields objectForKey:@"title"]];
        [currentTitle setLink:[currentFields objectForKey:@"link"]];
        [currentTitle setCategory:[currentFields objectForKey:@"category"]];
        [currentTitle setDescription:[currentFields objectForKey:@"description"]];

        NSString *beginString = [currentFields objectForKey:@"pubDate"];
        NSDate *beginDate = [dateFormatter dateFromString:beginString];
        [currentTitle setPubDate:beginDate];
        [news addObject:currentTitle];
        currentTitle = nil;
        currentFields = nil;

    } else if (currentFields && currentString) {

        NSString *trimmed;
        trimmed = [currentString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        [currentFields setObject:trimmed forKey:elementName];

    }
    currentString = nil;
}


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

    if (!currentString) {

        currentString = [[NSMutableString alloc]init];
    }
        [currentString appendString:string];

}

Solution

  • I think I have found my solution

    lastNews *item = [news objectAtIndex:indexPath.row];
        cell.titleLabel.text = item.title;