iOS从RSS Feed获取日期字符串

时间:2012-11-25 23:51:18

标签: ios xcode rss ios6 nsattributedstring

我有一个XML解析器,它使用RaptureXML来抓取每个项目并在表格单元格中显示它。我已经可以获得标题和描述,但我似乎无法弄清楚如何获得日期。

这是我到目前为止的日期:

NSString* dateString;

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEE MMMM d, YYYY"];
dateString = [formatter stringFromDate:self.pubDate];


[articleAbstract appendAttributedString:
 [[NSAttributedString alloc] initWithString: dateString]
 ];

我一直在调试器中显示:

'NSInvalidArgumentException', reason: 'NSConcreteAttributedString initWithString:: nil value'

我不知道我做错了什么。这是完整的代码:

RSSItem.h

#import <Foundation/Foundation.h>

@interface RSSItem : NSObject

@property (strong, nonatomic) NSString* title;
@property (strong, nonatomic) NSString* description;
@property (strong, nonatomic) NSURL* link;
@property (strong, nonatomic) NSAttributedString* cellMessage;
@property (strong, nonatomic) NSDate* pubDate;
@property (strong, nonatomic) NSString* dateString;

@end

RSSItem.m

#import "RSSItem.h"
#import "GTMNSString+HTML.h"

@implementation RSSItem

-(NSAttributedString*)cellMessage

{         if(_cellMessage!= nil)return _cellMessage;

NSDictionary* boldStyle = @{NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Bold" size:16.0]};
NSDictionary* normalStyle = @{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:16.0]};

NSMutableAttributedString* articleAbstract = [[NSMutableAttributedString alloc] initWithString:self.title];

[articleAbstract setAttributes:boldStyle
                         range:NSMakeRange(0, self.title.length)];


[articleAbstract appendAttributedString:
 [[NSAttributedString alloc] initWithString:@"\n\n"]
 ];

// Parse for date

NSString* dateString;

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEE MMMM d, YYYY"];
dateString = [formatter stringFromDate:self.pubDate];


[articleAbstract appendAttributedString:
 [[NSAttributedString alloc] initWithString: dateString]
 ];

[articleAbstract appendAttributedString:
 [[NSAttributedString alloc] initWithString:@"\n\n"]
 ];

int startIndex = [articleAbstract length];

NSString* description = [NSString stringWithFormat:@"%@...", [self.description substringToIndex:100]];
description = [description gtm_stringByUnescapingFromHTML];

[articleAbstract appendAttributedString:
 [[NSAttributedString alloc] initWithString: description]
 ];

[articleAbstract setAttributes:normalStyle
                         range:NSMakeRange(startIndex, articleAbstract.length - startIndex)];

_cellMessage = articleAbstract;
return _cellMessage;

}

@end

编辑:这是我的RSS Loader

#import "RSSItem.h"

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

@implementation RSSLoader

-(void)fetchRssWithURL:(NSURL*)url complete:(RSSLoaderCompleteBlock)c
{
dispatch_async(kBgQueue, ^{

    //work in the background
    RXMLElement *rss = [RXMLElement elementFromURL: url];
    RXMLElement* title = [[rss child:@"channel"] child:@"title"];        
    RXMLElement* pubDate = [[rss child:@"channel"] child:@"pubDate"];

    NSString *usableDate = [pubDate];       

    NSArray* items = [[rss child:@"channel"] children:@"item"];

    NSMutableArray* result = [NSMutableArray arrayWithCapacity:items.count];

    //more code
    for (RXMLElement *e in items) {

        //iterate over the articles
        RSSItem* item = [[RSSItem alloc] init];
        item.title = [[e child:@"title"] text];            
        item.pubDate = usableDate
        item.description = [[e child:@"description"] text];
        item.link = [NSURL URLWithString: [[e child:@"link"] text]];
        [result addObject: item];
    }

    c([title text], result);
});

}

有人可以帮我正确解析我的约会对象吗?

3 个答案:

答案 0 :(得分:2)

看起来stringFromDate返回nil。

如果您的意思是YYYY是年份,那就不正确,您应该使用yyyy代替。但我不认为日期格式存在问题。也许pubDate是零?

答案 1 :(得分:1)

根据您添加到问题中的代码,问题与您设置pubDate属性的方式有关。您将该属性定义为NSDate,但您在RSS加载程序代码中为该属性设置了NSString值。

您需要将从XML文件获取的字符串转换为NSDate,或将pubDate属性从NSDate更改为NSString。如果您执行后者,则需要更新您之前的代码以反映此更改。

决定实际上取决于您对日期的需求。

答案 2 :(得分:1)

你必须保留日期对象。无论何时你将日期对象分配给另一个字符串或日期格式化器,只需给出类似[date retain]。它可以工作