2013-04-01 31 views
0

我有以下問題:我正在構建一個應用程序,它是一個電視指南。我正在解析互聯網上xml文件中的頻道列表。這是我的代碼:如何根據Xcode中的當前日期解析不同的文件?

-(void)loadListing 
{ 
    NSURL *urlListing = [NSURL URLWithString:@"http://pik.bg/TV/bnt1/29.03.2013.xml"]; 

    NSData *webDataListing = [NSData dataWithContentsOfURL:urlListing]; 

    NSString *xPathQueryListing = @"//elem/title"; 

    TFHpple *parserListing = [TFHpple hppleWithXMLData:webDataListing]; 

    NSArray *arrayListing = [parserListing searchWithXPathQuery:xPathQueryListing]; 

    NSMutableArray *newArrayListing = [[NSMutableArray alloc] initWithCapacity:0]; 

    for (TFHppleElement *element in arrayListing) 
    { 
     Listing *shows = [[Listing alloc] init]; 
     [newArrayListing addObject:shows]; 
     shows.broadcast = [[element firstChild] content]; 
    } 

    _shows = newArrayListing; 
    [self.tableView reloadData]; 
} 

看第一行 - 我的文件的名稱是/.../01.04.2013.xml 明天的文件將/.../02.04.2013.xml等 如何根據當前日期來解析不同的文件?像這樣:今天解析/.../01.04.2013,明天將解析/.../02.04.2013等?提前致謝!

+1

找出當前的日期,然後傳遞當前日期urlString串..... –

回答

1
  1. 首先,使用URL中使用的相同格式獲取今天的日期。 (你有獨立的datemonthyear元器件起到)

  2. 然後,NSStringNSString *strToDay = [NSString stringWithFormat:@http://pik.bg/TV/bnt1/%@.xml",strToDay];

  3. 使用字符串轉換該日期爲NSString對象

  4. 形式進入NSURL,喜歡;如果您的網址包含由您指定的日期格式 NSURL *urlListing = [NSURL URLWithString:strToDay];

注意此解決方案纔有效。

+0

謝謝你,我的朋友,這解決了我的問題!這並不像我想的那麼難。 – scourGINHO

+1

很高興我能幫忙:) – viral

0

您可以使用NSDateFormatter配置的屬性來生成適當格式的字符串。使用[NSDate date]返回的NSDate實例獲取今天的日期,並使用格式化程序生成字符串。最後,將日期的字符串表示插入到URL字符串中,並從中構建一個NSURL

// Assuming the TV schedule is derived from the Gregorian calendar 
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

// Use the user's time zone 
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone]; 

// Instantiate a date formatter, and set the calendar and time zone appropriately 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setCalendar:gregorianCalendar]; 
[dateFormatter setTimeZone:localTimeZone]; 

// set the date format. Handy reference here: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns 
[dateFormatter setDateFormat:@"dd.MM.yyyy"]; 

// [NSDate date] returns a date corresponding to 'right now'. 
// Since we want to load the schedule for today, use this date. 
// stringFromDate: converts the date into the format we have specified 
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]]; 

// insert the date string into the URL string and build the URL 
NSString *URLString = [NSString stringWithFormat:@"http://pik.bg/TV/bnt1/%@.xml", dateString]; 
NSURL *URL = [NSURL URLWithString:URLString]; 

NSLog(@"URL = %@", URL); 
相關問題