我有一个 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
{ 如果 (_cellMessage!=nil) 返回 _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 加载器
#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);
});
}
有人可以帮我正确解析我的日期吗?
最佳答案
看起来 stringFromDate 返回 nil。
如果您的意思是 YYYY 是年份,那是不正确的,您应该改用 yyyy。但我不认为日期格式有问题。也许 pubDate 为零?
关于iOS 从 RSS Feed 获取日期字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13556884/