这里有几个主题讨论在向日历方法发送nil日期时在控制台中弹出的新iOS 6 Xcode错误:
-[__NSCFCalendar components:fromDate:toDate:options:]: fromDate cannot be nil
I mean really, what do you think that operation is supposed to mean with a nil fromDate?
An exception has been avoided for now.
A few of these errors are going to be reported with this complaint, then further
violations will simply silently do whatever random thing results from the nil.
Run Code Online (Sandbox Code Playgroud)
这个错误让我笑了一下,但让我思考......
我编写的导致此错误的代码从大量来源(从日期格式化程序,字符串,用户输入等)获取日期信息,我真的不希望每个提供的日期都有效 - 我知道完全有些人将是零,我很高兴处理这些对象的结果.
当我第一次开始使用Objective C编程时,我读到的一个功能就是向nil对象发送消息.这很棒,因为这意味着我不必担心传递零对象.
现在我收到了来自Xcode的一个脾气暴躁的错误消息告诉我,我已经向一个零对象发送了一条消息.
现在推断我必须在方法中使用它之前检查每个对象是否为零?我是否在做与nil对象完全令人发指的事情?