【问题标题】:Problems converting an NSString into an NSDate将 NSString 转换为 NSDate 的问题
【发布时间】:2012-08-10 15:50:45
【问题描述】:

我在将 NSString 转换为 NSDate 时遇到了一些问题。

这是我的代码:

NSLog(@"Unformatted date: %@", [d objectForKey:@"pubDate"]);

// Set the publication date as an NSDate
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
}
[self setPublicationDate:[dateFormatter dateFromString:[d objectForKey:@"pubDate"]]];

NSLog(@"Date object: %@", [self publicationDate]);
NSLog(@"Date object description: %@", [[self publicationDate] description]);

当它运行时,它会记录这些消息(它运行了几次,我只包含了两组它的日志消息):

2012-08-14 12:47:28.446 (App name removed)[9009:707] Unformatted date: Mon, 02 Apr 2012
2012-08-14 12:47:28.454 (App name removed)[9009:707] Date object: (null)
2012-08-14 12:47:28.460 (App name removed)[9009:707] Date object description: (null)
2012-08-14 12:47:28.467 (App name removed)[9009:707] Unformatted date: Sun, 25 Mar 2012
2012-08-14 12:47:28.474 (App name removed)[9009:707] Date object: (null)
2012-08-14 12:47:28.480 (App name removed)[9009:707] Date object description: (null)

我正在运行 iOS 5.1.1 的 iPad touch 上测试此代码

我能够更改接收日期的格式,它当前是从 PHP 日期函数发送的,"D, d M Y" 用作格式字符串。

谁能看看我哪里出错了?感谢您的帮助。

更新 1: 我已经切换并在模拟器上而不是在设备上运行它,并且由于某种原因它可以工作。关于为什么它不能在设备上运行的任何建议?模拟器运行 iOS 5.1,设备运行 iOS 5.1.1

更新 2:有人要求提供更多信息,publicationDate 是一个综合属性。

在.h文件中:

@property (nonatomic, strong) NSDate *publicationDate;

.m 文件中:

@synthesize publicationDate;

我已经进一步分解了代码:

NSLog(@"Unformatted date: %@", [d objectForKey:@"pubDate"]);
NSLog(@"Unformatted date class type: %@", [[d objectForKey:@"pubDate"] class]);

// Set the publication date as an NSDate
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
}
NSString *dateString = [d objectForKey:@"pubDate"];
NSLog(@"dateString: %@", dateString);

NSDate *formattedDate = [dateFormatter dateFromString:dateString];
NSLog(@"formattedDate %@", formattedDate);
[self setPublicationDate:formattedDate];

NSLog(@"Date object: %@", [self publicationDate]);
NSLog(@"Date object description: %@", [[self publicationDate] description]);

它现在记录:

2012-08-14 13:45:35.335 (app name removed)[9167:707] Unformatted date: Sun, 25 Mar 2012
2012-08-14 13:45:35.342 (app name removed)[9167:707] Unformatted date class type: __NSCFString
2012-08-14 13:45:35.347 (app name removed)[9167:707] dateString: Sun, 25 Mar 2012
2012-08-14 13:45:35.353 (app name removed)[9167:707] formattedDate (null)
2012-08-14 13:45:35.359 (app name removed)[9167:707] Date object: (null)
2012-08-14 13:45:35.365 (app name removed)[9167:707] Date object description: (null)

更新 3:成功!通过像这样更改日期格式化程序:

// Set the publication date as an NSDate
static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
}

现在可以了。我没想到要提到正在测试的设备设置为荷兰设置(因为这是一家荷兰开发公司)。抱歉,我从来没有想过这可能会影响这里的代码的工作方式。

【问题讨论】:

  • 你为什么要检查 if(!dateFormatter) ?前一行是 dateformatter 总是设置为零?
  • @Andy 你错了,只有第一次 dateformatter == nil,因为 dateformatter 是静态变量。
  • 因为它是一个静态变量,所以启动该变量的代码行只运行一次。
  • 显示你的 setPublicationDate 方法
  • 合成自:@property (nonatomic, strong) NSDate *publicationDate;@synthesize publicationDate;

标签: objective-c ios cocoa-touch nsdate nsdateformatter


【解决方案1】:

[d objectForKey:@"pubDate"] 的值不是正确的日期时间格式。

使用标准时间格式并得到这个输出..(把它当成eg)

NSDateFormatter *dateFormatter = nil;
if (!dateFormatter)  
{  
    dateFormatter = [[NSDateFormatter alloc] init];  
    [dateFormatter setDateFormat: @"EEE, dd MMM yyyy"];  
}

NSLog(@"todays date: %@", [NSDate date]);  
NSLog(@"formated date: %@", [dateFormatter stringFromDate:[NSDate date]]);

输出:

todays date: 2012-08-14 11:23:37 +0000  
formated date: Tue, 14 Aug 2012

【讨论】:

    【解决方案2】:

    你确定你从下面的代码中得到一个字符串对象吗?

    [d objectForKey:@"pubDate"]
    

    尝试记录从 d 返回的对象的类。否则代码看起来没问题。

    编辑: 检查设备中的区域设置:转到设备中的设置、常规、国际。如果区域格式未设置为英语,则必须在 dateformatter 中使用 setLocale 方法 查看此问题的已接受答案以获取更多信息:How do you interpret dates with NSDateFormatter?

    【讨论】:

    • 我的代码的第一行:"NSLog(@"Unformatted date: %@", [d objectForKey:@"pubDate"]);"记录它,它会正确显示。
    • 您只是在记录对象。记录对象的类。 NSLog("%@",[d 类]);无论您传递什么对象,iApple 提供的解决方案都可以正常工作。所以检查一下。
    • [ class] 将记录对象的类。请注意,您没有记录变量的类型。
    • 好的,现在运行它。稍后将与您联系结果。谢谢
    • 好的,我添加了:NSLog(@"Unformatted date class type: %@", [[d objectForKey:@"pubDate"] class]); 生成:2012-08-14 13:21:30.720 (App name removed)[9039:707] Unformatted date class type: __NSCFString
    【解决方案3】:

    也许您将日期对象传递给dateFromString

    NSDate *rawDate = [d objectForKey:@"pubDate"];
    NSString *stringDate = [dateFormatter stringFromDate:rawDate];
    NSDate *newDate = [dateFormatter dateFromString:stringDate];
    

    另外,您应该单独检查操作dateFromString: 的结果(不在publicationDate 的赋值语句中)。

    【讨论】:

      【解决方案4】:

      我已经粘贴了您的代码并进行了少量修改,因为我没有实时数据并且它工作正常。 下面是示例代码...

      NSString *strDate = @"Mon, 02 Apr 2012";
      NSLog(@"Unformatted date: %@", strDate);
      
      // Set the publication date as an NSDate
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      [dateFormatter setDateFormat:@"EEE, dd MMM yyyy"];
      NSDate *publicationDate = [dateFormatter dateFromString:strDate];    
      NSLog(@"Date object description: %@", [publicationDate description]);
      

      替换下一行

      [self setPublicationDate:[dateFormatter dateFromString:[NSString stringWithFormat:@"%@",[d objectForKey:@"pubDate"]]]];
      

      使用您现有的代码并检查。

      [self setPublicationDate:[dateFormatter dateFromString:[d objectForKey:@"pubDate"]]];
      

      【讨论】:

      • 好的,现在试试这个。将立即与您联系结果。
      • 感谢您的建议,但我将行改为:[self setPublicationDate:[dateFormatter dateFromString:[NSString stringWithFormat:@"%@", [d objectForKey:@"pubDate"]]]];,它仍然产生相同的结果。我做对了吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 2017-04-23
      • 2021-09-11
      相关资源
      最近更新 更多