2012-11-25 7 views
0

RaptureXML을 사용하여 각 항목을 가져 와서 표 셀 내에 표시하는 XML 파서가 있습니다. 이미 제목과 설명을 얻을 수는 있지만 날짜를 얻는 방법을 알아낼 수는 없습니다.RSS 피드에서 날짜 문자열 가져 오기

는 여기 날짜는 지금까지이 작업은 다음과 같습니다

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 = 전무) 반환 _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 

편집 : 여기

#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); 
}); 

} 

누군가가 나를 제대로 내 날짜를 구문 분석하는 데 도움 주실 래요 내 RSS 로더입니까?

+0

작동합니다. NSLog를 추가하십시오 (@ "% @ => % @", self.pubDate, dateString); 무엇이 null인지 알기 위해서 .... –

+0

@ Daij-Djan 나는 그것을 시도했고 null을 돌려 준다. 실제로 날짜를 어떻게 얻습니까? –

+0

'pubDate' 속성을 설정해야합니다. 당신이 그것에게 가치를 줄 때까지 그것은 'nil'이 될 것입니다. 게시 한 코드 중 어느 것도 속성을 설정하려고 시도하는 방법 (또는 설정하지 않는 방법)을 보여줍니다. – rmaddy

답변

1

질문에 추가 한 코드에 따라 문제는 pubDate 속성을 설정하는 방법과 관련이 있습니다. 속성을 NSDate으로 정의했지만 RSS 로더 코드의 속성에 NSString 값을 설정했습니다.

XML 파일에서 가져온 문자열을 NSDate로 변환하거나 속성을 NSDate에서 NSString으로 변경해야합니다. 후자를 수행하는 경우이 변경 사항을 반영하기 위해 이전 코드를 업데이트해야합니다.

결정은 실제로 당신이 날짜로 무엇을해야하는지에 달려 있습니다.

+0

날짜를 가져 와서 형식을 지정하고 싶습니다 : 2012 년 11 월 25 일 일요일. 문자열 형식으로 모든 작업을 수행 할 수 있습니까? –

+0

'item을 바꿔야합니다.pubDate = [[e child : @ "pubDate"] text];'다른 NSDateFormatter'를 사용하여 XML 파일에서 발견 된 문자열을 NSDate 객체로 변환하는 코드. 그런 다음'cellMessage'에있는 기존 코드를 사용하여'pubDate'를 원하는 날짜 문자열로 변환 할 수 있습니다. – rmaddy

+0

도움을 주셔서 감사합니다. –

2

stringFromDate처럼 보이는 것은 nil을 반환합니다.

YYYY 연도를 의미하는 경우 올바르지 않으므로 대신 yyyy를 사용해야합니다. 그러나 나는 날짜 형식으로 그 문제를 생각하지 않는다. 어쩌면 pubDate는 무효일까요?

+2

은 코멘트 여야합니다 .. 대답이 아닙니다 : P –

1

당신처럼 줄 당신이 다른 문자열 또는 날짜 포맷으로 날짜 객체를 할당합니다 object.Whenever 날짜를 유지해야 [날짜 유지] .IT는 stringFromDate이 전무를 반환