将NSString转换为NSDate时出现问题

时间:2012-08-14 10:59:13

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

我在将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"];
}

它现在有效。我没想到提到正在测试的设备被设置为荷兰语设置(因为这是一家荷兰开发公司)。道歉,我从未想过这可能会影响这里的代码如何运作。

4 个答案:

答案 0 :(得分:2)

您确定从以下代码获取字符串对象吗?

[d objectForKey:@"pubDate"]

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

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

答案 1 :(得分:1)

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

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"]]];

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

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

使用标准时间格式并获得此输出..(将其视为例如)

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