iphone - NSDate dateFromString 已弃用?

标签 iphone xcode nsdate xcode3.2

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

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

警告是:

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

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

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

最佳答案

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

最基本的用法是这样的:

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

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

NSDate *date = [dateFormatter dateFromString:dateString];

关于iphone - NSDate dateFromString 已弃用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3402367/

相关文章:

iphone - UIView 背景颜色影响 iOS 5 中的触摸

iphone - 应用程序在dealloc中崩溃

iphone - iOS 的 HTTP 中的 getReasonPhrase()

ios - 在 UIImage 周围绘制边框

swift - 发送到网络服务的日期错误

iphone - 更新 UITableView 数据

iphone - 我是否正确使用 CocosDenshion?

swift - 达到最大时间戳计数,不会记录更多事件 Xcode

swift - 迄今为止的数字 - Swift

iphone - 如何从 NSDate 中获取日期组件?