我在将 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"];
}
现在可以了。我没想到要提到正在测试的设备设置为荷兰设置(因为这是一家荷兰开发公司)。抱歉,我从来没有想过这可能会影响这里的代码的工作方式。