Xcode XML文件从网站解析

时间:2022-05-25 20:37:32

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

我的代码有问题,但无法理解问题所在。

I am parsing the following XML file

我正在解析以下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>

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

我得到了我的新闻的数组计数(它总是20),但当我尝试在我的自定义单元格中放置描述时,我的应用程序崩溃了:(

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'

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

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

如果我把静态文本@“测试”,那么一切都OK。

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

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

Thanks

My XML parser is here:

我的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];

}

2 个解决方案

#1


0  

This object lastNews is nil, that is why throwing exception "NSInvalidArgumentException".

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

#2


0  

I think I have found my solution

我想我找到了解决方案

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

#1


0  

This object lastNews is nil, that is why throwing exception "NSInvalidArgumentException".

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

#2


0  

I think I have found my solution

我想我找到了解决方案

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