对于iPhone OS 4.0" dateFromString" NSDateFormatter的方法返回nil [英] For iPhone OS 4.0 "dateFromString" method of NSDateFormatter returns nil

查看:135
本文介绍了对于iPhone OS 4.0" dateFromString" NSDateFormatter的方法返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码&它在iPhone OS 3.2中完美运行

I am using following code & its working perfectly fine in iPhone OS 3.2

+(NSDate *)NSDateFromString:(NSString *)dateString
{

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *dateObj=[dateFormatter dateFromString:dateString];
    [dateFormatter release], dateFormatter=nil;
    return dateObj;
}

但是当我尝试使用相同的代码我在iPhone OS 4.0上时dateObj是没有。

But when I tried to use the same code I iPhone OS 4.0 the dateObj was nil.

原因是:


- (id)init NSDateFormatter的方法仅适用于iPhone OS 2.0到iPhone OS 3.2。
在iPhone OS 4.0中已弃用。

- (id)init method for NSDateFormatter is only Available in iPhone OS 2.0 through iPhone OS 3.2. It is deprecated in iPhone OS 4.0.

现在有什么解决方案?如何初始化NSDateFormater?有什么替代方案?

Now what is the solution for that ? How to init NSDateFormater? What's the alternative ?

推荐答案

在iOS2.x和3.x中,NSDate description / datefromstring函数返回:
2010 -09-24 17:30:00 - 0700

In iOS2.x and 3.x the NSDate description/datefromstring function returns: 2010-09-24 17:30:00 - 0700

在iOS 4.1中它返回:
2010-09-25 00:30:00 GMT

in iOS 4.1 it returns this: 2010-09-25 00:30:00 GMT

你可以向苹果提交一个错误。它甚至没有遵循规范,该规范声明它应该返回0000作为偏移量。因此,它不仅不返回本地日期,它还使用GMT而不是偏移量。看来这种新格式与NSDateFormatter方法不兼容。

You could submit a bug to apple. It doesn't even follow the specification which states that it should return 0000 for the offset. So not only does it not return local date, it also uses GMT instead of an offset. It appears that this new format isn't compatible with NSDateFormatter methods.

另外,我认为这不是文档中的错误。在API Diff文档中,它列出了为NSDateFormatter删除了init。

Also, I don't think it is an error in documentation. In the API Diff document, it lists that init is removed for NSDateFormatter.

这篇关于对于iPhone OS 4.0" dateFromString" NSDateFormatter的方法返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆