简体   繁体   English

NSString到NSDate问题IOS7

[英]NSString to NSDate issue IOS7

I'm using the following code to convert NSString to NSDate : 我正在使用以下代码将NSString转换为NSDate

NSDate *date = [NSDate convertStringToDate:strDate andFormatter:@"MM dd"];

+ (NSDate *)convertStringToDate:(NSString *)dateString andFormatter:(NSString *)formatter
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    // this is imporant - we set our input date format to match our input string
    // if format doesn't match you'll get nil from your string, so be careful
    [dateFormatter setDateFormat:formatter];

    dateFormatter.timeZone = [NSTimeZone systemTimeZone];

    return [dateFormatter dateFromString:dateString];
}

NSString is @"07 15" . NSString是@"07 15" But my NSDate is nil . 但是我的NSDate nil On a simulator it is fine but on device with iOS7 it is nil. 在模拟器上可以,但在装有iOS7的设备上则为零。

Do you have any suggestions about it? 您对此有何建议?

If your code works in iOS 6, then this may just be a bug in iOS 7. Be sure to file a bug report if you think that's the case. 如果您的代码在iOS 6中可以运行,那么这可能只是iOS 7中的错误。如果您认为是这种情况,请务必提交错误报告。

If it doesn't work in iOS 6, then it may be that a month and day aren't sufficient to specify a date. 如果在iOS 6中不起作用,则可能是一个月和一天不足以指定日期。 Try adding a year and see if you get a valid date that way. 尝试添加一年,看看是否以这种方式获得了有效日期。

Note that questions about iOS 7 should be posted to Apple's developer forums, not here. 请注意,有关iOS 7的问题应发布在Apple的开发人员论坛上,而不是在此处。

you may need to set the locale like this: 您可能需要像这样设置语言环境:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

I solved this problem including the locale to the formatter. 我解决了这个问题,包括格式化程序的语言环境。 hope this helps. 希望这可以帮助。

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

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