代码之家  ›  专栏  ›  技术社区  ›  RoLYroLLs

XML中NSDateFormatter的空值

  •  0
  • RoLYroLLs  · 技术社区  · 16 年前

         NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"US"];
            [dateFormatter setLocale:locale];
            [dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss ZZZ"];
            currentDownloadedScore.dateEntered = [dateFormatter dateFromString:self.currentValue];
            NSLog(@"Date CV: %@",self.currentValue);
            NSLog(@"Date: %@",[currentDownloadedScore.dateEntered description]);
            [locale release];
            [dateFormatter release];

    以下是调试器中的结果:

    2010-07-16 08:15:35.741 MyApp[75003:207] Date CV: 07/16/2010 04:21:00 +00:00
    2010-07-16 08:15:35.742 MyApp[75003:207] Date: (null)
    2010-07-16 08:15:35.745 MyApp[75003:207] Date CV: 07/16/2010 01:09:26 +00:00
    2010-07-16 08:15:35.749 MyApp[75003:207] Date: (null)
    

    3 回复  |  直到 16 年前
        1
  •  2
  •   RoLYroLLs    16 年前

    我发现了问题。好像 ZZZ 格式化的一部分不再接受冒号 : 在当时。

    07/16/2010 04:21:00 +0000

    不起作用: 07/16/2010 04:21:00 +00:00

    +00:00 把绳子分开,换成 +0000 . 塔达!真管用!

        2
  •  0
  •   Community Mohan Dere    9 年前

    NSDateFormatter在iOS4中变得非常挑剔。 This other SO question

        3
  •  0
  •   scottsanders    16 年前

    [currentDate replaceOccurrencesOfString:@"\n" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0,[currentDate length])];