Iphone 如何根据Xcode中的当前日期解析不同的文件?

Iphone 如何根据Xcode中的当前日期解析不同的文件?,iphone,xml,algorithm,parsing,Iphone,Xml,Algorithm,Parsing,我有以下问题:我正在构建一个作为电视指南的应用程序。我正在从internet上的xml文件解析频道列表。这是我的代码: -(void)loadListing { NSURL *urlListing = [NSURL URLWithString:@"http://pik.bg/TV/bnt1/29.03.2013.xml"]; NSData *webDataListing = [NSData dataWithContentsOfURL:urlListing]; NSSt

我有以下问题:我正在构建一个作为电视指南的应用程序。我正在从internet上的xml文件解析频道列表。这是我的代码:

-(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等等?提前谢谢

  • 首先,使用URL中使用的相同格式获取今天的日期。(您必须使用单独的
    日期
    月份
    年份
    组件)

  • 然后,将该日期转换为
    NSString
    对象

  • 形成一个
    NSString
    NSString*stroday=[NSString]
    stringWithFormat:@http://pik.bg/TV/bnt1/%@.xml”,stroday];

  • 使用字符串进入
    NSURL
    ,如;
    NSURL*urlisting=[NSURL URLWithString:stroday];


  • 注意仅当URL包含您指定的日期格式时,此解决方案才起作用。

    您可以使用
    NSDateFormatter
    ,配置属性,以生成适当格式的字符串。使用
    [NSDate-date]返回的
    NSDate
    实例
    获取今天的日期,并使用格式化程序生成字符串。最后,将日期的字符串表示形式插入URL字符串,并从中构建一个
    NSURL

    //假设电视节目表是从公历派生的
    NSCalendar*gregorianCalendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    //使用用户的时区
    NSTimeZone*localTimeZone=[NSTimeZone localTimeZone];
    //实例化日期格式化程序,并适当设置日历和时区
    NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];
    [日期格式化程序setCalendar:gregorianCalendar];
    [dateFormatter setTimeZone:localTimeZone];
    //设置日期格式。此处有方便的参考:http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
    [日期格式化程序setDateFormat:@“dd.MM.yyyy”];
    //[NSDate date]返回与“立即”对应的日期。
    //由于我们要加载今天的计划,请使用此日期。
    //stringFromDate:将日期转换为我们指定的格式
    NSString*dateString=[dateFormatter stringFromDate:[NSDate date]];
    //将日期字符串插入URL字符串并生成URL
    NSString*URLString=[NSString stringWithFormat:@]http://pik.bg/TV/bnt1/%@.xml“,日期字符串];
    NSURL*URL=[NSURL URLWithString:URLString];
    NSLog(@“URL=%@”,URL);
    
    找出当前日期,然后将当前日期的字符串传递给urlString…..谢谢,我的朋友,这解决了我的问题!这并不像我想的那么难。