Submit NSCFCalendar nil NSDate Sarcastic Error

Several threads discuss the new Xcode iOS 6 bug that appears in the console when sending nil dates to calendar methods:

-[__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. 

The mistake made me laugh a little, but it made me think ...

The code I wrote that leads to this error accepts date information from a variety of sources (from date formats, strings, user input, etc.), and I really don't expect every dated date to be valid - I'm fine I know that some of them will be zero, and I am very pleased with the results of such objects.

When I first started programming in Objective-C, one of the functions I read was sending messages to Nile objects . This was great because it meant that I didn't have to worry about walking around zero objects.

Now I have a rude error message from Xcode telling me that I sent a message to the nil object.

Is the conclusion that I should check every object non-zero before using it in methods? Am I doing something completely disgusting when working with null objects?

+2
source share
2 answers

Is the conclusion that I should check every object non-zero before using it in methods?

No, this is not a conclusion. The conclusion is that you need to check the link for the date that you send to NSCalendar , because the result of sending the link to zero is undefined. At the moment, this may work for you, but Apple may change the behavior in a future release to do whatever it wants, right up to including child print. Oh don't you think about children?

Am I doing something completely disgusting when working with null objects?

Not. It is 100% fine to send a message to the receiver , which can be zero if the result you want to return is zero or zero in this case. In this case, you do not send the message to zero. You submit an argument for a non-grassroots receiver who did not expect it. In one case, the receiver is zero; otherwise, the argument is zero.

+6
source

Now I have a rude error message from Xcode telling me that I sent a message to the nil object.

Wrong. Your NSCalendar (receiver) is not zero. That nil is an NSDate that you pass as one of the arguments to the method. NSCalendar has no idea what to do when you give it nil NSDate , so it gives you a log message, rather than throwing an exception and crashing your application.

+1
source

All Articles