简体   繁体   English

将NSString转换为NSDate时出现问题

[英]Problems converting an NSString into an NSDate

I'm having some problems getting an NSString to convert to an NSDate. 我在获取NSString转换为NSDate时遇到一些问题。

Here is my code: 这是我的代码:

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

When this is run, it logs these messages (it is run several times, I've included just two sets of its log messages): 运行此命令时,它将记录以下消息(运行了几次,我仅包含了两组日志消息):

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)

I'm testing this code on an iPad touch running iOS 5.1.1 我正在运行iOS 5.1.1的iPad touch上测试此代码

I am able to change the format of the date being received, it is currently being sent from a PHP date function with "D, d MY" being used as the format string. 我可以更改接收日期的格式,当前它是从PHP日期函数发送的,其中"D, d MY"用作格式字符串。

Can anyone please see where I am going wrong? 谁能知道我要去哪里了吗? Thank you for your help. 谢谢您的帮助。

UPDATE 1: I've switched over and ran it on the simulator instead of on a device and for some reason it's worked. 更新1:我已经切换并在模拟器上而不是在设备上运行它,由于某种原因,它可以工作。 Any suggestions as to why it isn't working on the device? 关于它为何无法在设备上运行的任何建议? Simulator is running iOS 5.1 and the device is running iOS 5.1.1 模拟器正在运行iOS 5.1,设备正在运行iOS 5.1.1

UPDATE 2: Some people have requested more information, publicationDate is a synthesised property. 更新2:有些人要求提供更多信息, publicationDate是一个综合属性。

In the .h file: 在.h文件中:

@property (nonatomic, strong) NSDate *publicationDate;

In the .m file: 在.m文件中:

@synthesize publicationDate;

I've broken the code up even further: 我进一步细分了代码:

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

and it now logs: 现在它记录:

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)

UPDATE 3: Success! 更新3:成功! By changing the date formatter like so: 通过像这样更改日期格式器:

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

It now works. 现在可以使用了。 I didn't think to mention that the device being tested on is set to Dutch settings (as this is a Dutch development company). 我没想到要提到要测试的设备设置为荷兰设置(因为这是一家荷兰开发公司)。 Apologies, it never occurred to me that this might effect how the code here worked. 抱歉,这从未影响过我的代码的工作方式。

Are you sure you are getting a string object from the following code? 您确定从以下代码中获取字符串对象吗?

[d objectForKey:@"pubDate"]

Try logging the class of the object returned from d . 尝试记录从d返回的对象的类。 Otherwise the code seems alright. 否则,代码看起来还不错。

Edit: Check the locale settings in your device: Go to Settings, General, International in your device. 编辑:检查设备中的区域设置:转到设备中的“设置”,“常规”,“国际”。 If Region Format is not set to English you will have to use the setLocale method in the dateformatter Check the accepted answer for this question for more info: How do you interpret dates with NSDateFormatter? 如果“区域格式”未设置为英语,则必须在dateformatter中使用setLocale方法。有关此问题的更多信息,请检查接受的答案: 如何使用NSDateFormatter解释日期?

I have paste your code and have done minor modifications as I don't have live data and it works fine. 我已经粘贴了您的代码,并做了一些小的修改,因为我没有实时数据,并且工作正常。 Below is that sample code... 下面是该示例代码...

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

Replace below line 替换下面的行

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

with your existing code and check. 与您现有的代码并检查。

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

Maybe you are passing a date object to to dateFromString . 也许您dateFromString日期对象传递给dateFromString

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

Also, you should check the result of the operation dateFromString: separately (not within the assign statement for publicationDate ). 另外,您应该单独检查操作dateFromString:的结果(不在publicationDate dateFromString:语句中)。

The value for [d objectForKey:@"pubDate"] is not in proper date time format. [d objectForKey:@“ pubDate”]的值格式不正确。

Used standard time format and got this output.. (took it as 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]]);

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM