ios - 当 NSDate 为 nil 时,NSDateFormatter 返回(空)字符串

标签 ios objective-c nsstring nsdate nsdateformatter

我使用日期格式化程序来获取新字符串,但在某些情况下 startDate 可以为 nil。

resultDate = [NSString stringWithFormat:@"%@", [formatter stringFromDate:startDate]];

然后 resultDate 获取 (null) 字符串 (NSTaggedPointerString),但如果格式化程序获取 nil 作为日期参数,我希望只获取 nil。

最佳答案

目前接受的答案实际上并不正确。

stringFromDate 确实在您将 nil 日期传递给它时返回 nil

你的字符串是 "(null)"的原因是因为 [NSString stringWithFormat:"%@", nil] 返回字符串 "(null)"

关于ios - 当 NSDate 为 nil 时,NSDateFormatter 返回(空)字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33829127/

相关文章:

ios - 将 NS_OPTIONS 枚举 (UIRemoteNotificationType) 位掩码转换为 NSString 分隔值

IOS - 位置更新间隔

ios - Launchscreen.storyboard 横向启动画面

objective-c - 使用 Interface Builder、NSObjectController 子类和绑定(bind)时在模型和 View 之间插入 Controller 逻辑

objective-c - Mac应用程序中的本地推送通知

xcode - 为什么NSString使我的Release版本崩溃,但是在Xcode里面可以工作?

ios - 如何在没有 Storyboard的情况下调整 iPad 和 iPhone 4S 的 View 大小?

ios - UINavigationController.hidesBarsOnSwipe=YES 导致 NSInternalInconsistencyException

ios - Touchsbegan 不适用于第二个 View Controller 的 subview

iphone - NSDateFormatter返回空值dateFromString:iPhone?