0

次の問題があります: TV ガイドのアプリを作成しています。インターネット上の 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 などを解析します。前もって感謝します!

4

2 に答える 2

0

NSDateFormatter構成されたプロパティを使用して、適切な形式の文字列を生成できます。NSDateによって返されたインスタンスを使用し[NSDate date]て今日の日付を取得し、フォーマッタを使用して文字列を生成します。最後に、日付の文字列表現を 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);
于 2013-04-01T10:31:14.283 に答える