NSDate dateFromString 已弃用?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSDateFormatter
是您从NSString
获取NSDate
的预期方法。最基本的用法是这样的:
NSDateFormatter
is the intended way for you to get anNSDate
from anNSString
.The most basic usage is something like this:
我有同样的问题。
我将 dateFormat 从 @"YYYY/M/d H:m:s" 更改为 @"YYYY/MM/dd HH:mm:ss" 如下。
然后它可以在我的 iPhone 4 上运行。(适用于 iOS 6 的 Xcode 4.5。)
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.)
嗨,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.