0

我的代码有问题,但不明白问题出在哪里。

我正在解析以下 XML 文件

  <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>

我正在获取我的新闻的数组计数(始终为 20),但是当我尝试在我的自定义单元格中添加描述时,我的应用程序崩溃了 :(

这是错误:

2013-04-17 23:52:05.258 IsraelNews[20379:c07] -[lastNews isEqualToString:]:无法识别的选择器发送到实例 0x715b460 2013-04-17 23:52:05.259 IsraelNews[20379:c07] *终止应用程序未捕获的异常“NSInvalidArgumentException”,原因:“-[lastNews isEqualToString:]:无法识别的选择器发送到实例 0x715b460”

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

如果我把静态文本@"Test",那么一切都好。

我做错了什么?如果您需要任何其他信息,请告诉我。

谢谢

我的 XML 解析器在这里:

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

}
4

2 回答 2

0

这个对象lastNews是 nil,这就是抛出异常“NSInvalidArgumentException”的原因。

于 2013-04-17T22:17:32.973 回答
0

我想我找到了我的解决方案

lastNews *item = [news objectAtIndex:indexPath.row];
    cell.titleLabel.text = item.title;
于 2013-04-18T05:14:49.383 回答