NSDate dateFromString 已弃用?

发布于 2024-09-13 06:35:59 字数 382 浏览 2 评论 0原文

我正在尝试使用 NSDate dateFromString 方法,但收到警告并且它导致应用程序崩溃。代码如下所示:

NSString *pickerDate = [NSString stringWithFormat:@"%@", timeSelector.date];
NSDate *defaultDate = [NSDate dateFromString:pickerDate];

警告是:

'NSDate' may not respond to '+dateFromString'.

看来该方法已被弃用(在从 XCode 2 升级到 3 的过程中)。

我可以使用什么替代方法从字符串创建日期?

I'm trying to use the NSDate dateFromString method but I'm getting an warning and it's crashing the app. The code looks like:

NSString *pickerDate = [NSString stringWithFormat:@"%@", timeSelector.date];
NSDate *defaultDate = [NSDate dateFromString:pickerDate];

The warning is:

'NSDate' may not respond to '+dateFromString'.

It appears that method is deprecated (in the midst of an upgrade from XCode 2 to 3.

What alternate method can I use to create a date from a string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇纵 2024-09-20 06:35:59

NSDateFormatter 是您从 NSString 获取 NSDate 的预期方法。

最基本的用法是这样的:

NSString *dateString = @"3-Aug-10";

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"d-MMM-yy";

NSDate *date = [dateFormatter dateFromString:dateString];

NSDateFormatter is the intended way for you to get an NSDate from an NSString.

The most basic usage is something like this:

NSString *dateString = @"3-Aug-10";

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"d-MMM-yy";

NSDate *date = [dateFormatter dateFromString:dateString];
惯饮孤独 2024-09-20 06:35:59

我有同样的问题。
我将 dateFormat 从 @"YYYY/M/d H:m:s" 更改为 @"YYYY/MM/dd HH:mm:ss" 如下。
然后它可以在我的 iPhone 4 上运行。(适用于 iOS 6 的 Xcode 4.5。)


NSDateFormatter *dateFormatter1;
NSString *dateStr1=@"";
NSDate *gantanDate1;

dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setLocale:[NSLocale systemLocale]]; 
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]]; 
dateFormatter1.dateFormat=@"YYYY/MM/dd HH:mm:ss";
dateStr1=@"2012/1/1 00:00:00"];
//  in his case,   dateStr1=pickerDate;  
gantanDate1 = [dateFormatter1 dateFromString:dateStr1];

I had a same problem.
I changed the dateFormat from @"YYYY/M/d H:m:s" to @"YYYY/MM/dd HH:mm:ss" as follows.
Then it works on my iPhone 4. (Xcode 4.5 for iOS 6.)


NSDateFormatter *dateFormatter1;
NSString *dateStr1=@"";
NSDate *gantanDate1;

dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setLocale:[NSLocale systemLocale]]; 
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]]; 
dateFormatter1.dateFormat=@"YYYY/MM/dd HH:mm:ss";
dateStr1=@"2012/1/1 00:00:00"];
//  in his case,   dateStr1=pickerDate;  
gantanDate1 = [dateFormatter1 dateFromString:dateStr1];
海之角 2024-09-20 06:35:59

嗨,Silber,每个人都说同样的事情将字符串日期转换为日期对象。试试这个,我认为您在将日期保存到字符串并从字符串获取日期的两个地方使用了两个日期格式化程序。正确尝试使用相同的日期格式化程序在这两个地方。它会解决你的问题。

Hi Silber everyone says the same thing to convert the string date to date object.Try this i think you have used two date formatters in two places where you are saving date to string and getting date from string.right try to use the same date formatters in both places. It will solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文