我在将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日期函数发送的,其中_2942246被用作格式字符串 .
任何人都可以看到我错在哪里?谢谢您的帮助 .
更新1:我已经切换并在模拟器上而不是在设备上运行它,并且由于某种原因它可以工作 . 有关为什么它不在设备上工作的任何建议? Simulator运行iOS 5.1,设备运行iOS 5.1.1
更新2:有些人要求提供更多信息, publicationDate
是综合 property .
在.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 回答
您确定从以下代码获取字符串对象吗?
尝试记录从
d
返回的对象的类 . 否则代码似乎没问题 .编辑:检查设备中的区域设置:转到设备中的设置,常规,国际 . 如果区域格式未设置为英语,则必须在dateformatter中使用setLocale方法检查此问题的已接受答案以获取更多信息:How do you interpret dates with NSDateFormatter?
我已粘贴您的代码,并进行了少量修改,因为我没有实时数据,它工作正常 . 以下是示例代码......
替换下面的行
使用现有代码并检查 .
也许你正在将日期对象传递给
dateFromString
.此外,您应该单独检查操作
dateFromString:
的结果(不在publicationDate
的assign语句中) .[d objectForKey:@"pubDate"] 的值不是正确的日期时间格式 .
Used 标准时间格式并获得此输出..(以此为例)
Output: