首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 5突破NSDateFormatter?

iOS 5突破NSDateFormatter?
EN

Stack Overflow用户
提问于 2012-01-10 19:04:52
回答 2查看 370关注 0票数 0

我对NSDateFormatter和iOS有意见。实际上,我在这里找到了对问题的认识和解释:

http://www.cocoanetics.com/2011/12/ios-5-breaking-nsdateformatter/

然而,我的大脑无法处理这些信息,无法将其转换为针对我特定情况的解决方案。我希望你们能帮我一把:

有人能告诉我如何在iOS5中使用下面的代码吗?它曾经运行得很好:

代码语言:javascript
复制
NSLocale *myLocale = [[NSLocale alloc] initWithLocaleIdentifier:@”en_GB”];
[inputFormatter setLocale: myLocale];
[myLocale release];
[inputFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *formattedDate = [inputFormatter dateFromString: feedDateString];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"EEEE d MMMM"];

当我记录outputFormatter字符串formattedDate:formattedDate I get null时,超出我控制范围的输入字符串将类似于“Tue,03 Jan 2012 00:00:00 +0100”

最初的语言环境是“en_US”,但我更改了它,希望这会有所帮助。

它也会帮助这个帖子的人,这似乎是一个死胡同:

NSDateFormatter on ios 5 - any other way to use?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-13 19:15:53

对于所有在这上面花了脑筋的人:这都是我愚蠢的错误:需要格式化的输入字符串不是"Mon,13Feb 2012 00:00:00 +0100“,而是"Mon,13 Feb 2012 00:00:00 +0100”,看到了吗?嗯,在NSLogging中我没有看到尾随的空格。讽刺的是:字符串后面的许多空格和回车在这里也没有显示!

不知何故,iOS 4并不关心这一点。iOS5要挑剔得多,而且很可能是这样。所以我按如下方式解决了这个问题:

代码语言:javascript
复制
NSString *trimmedString = [inputstring stringByTrimmingCharactersInSet:
                               [NSCharacterSet whitespaceAndNewlineCharacterSet]];

我希望这也能对你有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2012-01-26 19:42:35

试试这个:

代码语言:javascript
复制
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
        
NSString *str = [outputFormatter stringFromDate:[NSDate date]];
/* 
   Do something with this string
*/
[outputFormatter release];

您也可以通过向NSDateFormatter添加setLocale参数来添加区域。

代码语言:javascript
复制
NSLocale *myLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
[outputFormatter setLocale: myLocale];
        
NSString *str = [outputFormatter stringFromDate:[NSDate date]];

/* 
   Do something with this string
*/

[outputFormatter release];
[myLocale release];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8802254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档