iOS从RSS Feed获取日期字符串 [英] iOS getting date string from RSS Feed
问题描述
我有一个XML解析器,它使用RaptureXML来抓取每个项目并在表格单元格中显示它。我已经可以获得标题和描述,但我似乎无法弄清楚如何获取日期。
这是迄今为止我所拥有的日期:
NSString * dateString;
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[格式化程序setDateFormat:@EEEE MMMM d,YYYY];
dateString = [formatter stringFromDate:self.pubDate];
[articleAbstract appendAttributedString:
[[NSAttributedString alloc] initWithString:dateString]
];
我一直在调试器中显示:
'NSInvalidArgumentException',原因:'NSConcreteAttributedString initWithString :: nil value'
我不知道我做错了什么。以下是完整代码:
RSSItem.h
#import< Foundation / Foundation.h>
@interface RSSItem:NSObject
@property(强,非原子)NSString * title;
@property(强,非原子)NSString * description;
@property(强,非原子)NSURL *链接;
@property(强,非原子)NSAttributedString * cellMessage;
@property(强,非原子)NSDate * pubDate;
@property(强,非原子)NSString * dateString;
@end
RSSItem.m
#importRSSItem.h
#importGTMNSString + HTML.h
@implementation RSSItem
- (NSAttributedString *)cellMessage
{
if(_cellMessage!= nil)return _cellMessage;
NSDictionary * boldStyle = @ {NSFontAttributeName:[UIFont fontWithName:@ Helvetica-Bold尺寸:16.0]};
NSDictionary * normalStyle = @ {NSFontAttributeName:[UIFont fontWithName:@Helveticasize: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]
];
//解析日期
NSString * dateString;
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[格式化程序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
#importRSSItem.h
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)
@implementation RSSLoader
- (void)fetchRssWithURL:(NSURL *)url complete:(RSSLoaderCompleteBlock)c
{
dispatch_async(kBgQueue,^ {
//在后台工作
RXMLElement * rss = [RXMLElement elementFromURL:url];
RXMLElement * title = [[ rss child:@channel] child:@title];
RXMLElement * pubDate = [[rss child:@channel] child:@pubDate];
NSString * usefulDate = [pubDate];
NSArray * items = [[rss child:@channel] children:@item];
NSMutableArray * result = [NSMutableArray arrayWithCapacity:items.count];
//更多代码
for(RXMLElement * e in items){
//迭代文章
RSSItem * item = [[RSSItem alloc] init];
item.title = [[e child:@title] text];
item.pubDate = usefulDate
item.description = [[e child:@description] text];
item.link = [NSURL URLWithString:[[e child:@link] text]];
[结果addObject:item];
}
c([标题文本],结果);
});
}
有人可以帮我解析我的日期吗?
根据您添加到问题中的代码,问题与设置 pubDate的方式有关
属性。您将该属性定义为 NSDate
,但您在RSS加载程序代码中为该属性设置了 NSString
值。 / p>
您需要将从XML文件获取的字符串转换为NSDate或从<$ c更改 pubDate
属性$ c> NSDate 到 NSString
。如果您执行后者,则需要更新您之前的代码以反映此更改。
决定实际上取决于您需要对日期执行的操作。
I have an XML parser that uses RaptureXML to grab each item and display it within a table cell. I already can get the title and description, but I can't seem to work out how to get the date.
Here's what I have so far for the 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]
];
I keep getting this showing up in the debugger:
'NSInvalidArgumentException', reason: 'NSConcreteAttributedString initWithString:: nil value'
I don't know what I'm doing wrong. Here's the full code:
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
EDIT: Here is my 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);
});
}
Can someone please help me parse my date correctly?
Based on the code you added to your question, the problem has to do with how you set the pubDate
property. You defined the property to be an NSDate
but you set an NSString
value to the property in your RSS loader code.
You need to convert the string obtained from the XML file to an NSDate or change your pubDate
property from NSDate
to NSString
. If you do the latter then your earlier code needs to be updated to reflect this change.
The decision is really dependent on what you need to do with the date.
这篇关于iOS从RSS Feed获取日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!