Sending NSCFCalendar nil NSDate Sarcastic Error [closed]

一曲冷凌霜 提交于 2019-12-02 02:04:30

Is the inference now that I've got to check every object is not nil before using it in methods?

No, that is not the inference. The inference is that you have to check the date reference you're sending to NSCalendar, because the result of sending it a nil reference is undefined. It might work for you at the moment, but Apple could change the behavior in a future release to do whatever it wants, up to and including clubbing a baby seal. Oh, won't you please think of the baby seals?

Am I doing something totally heinous working with nil objects?

No. It's 100% fine to send a message to a receiver that might be nil, if the result you want back is nil or zero in that case. In this case, you're not sending a message to nil. You're sending a nil argument to a non-nil receiver that wasn't expecting it. In one case, the receiver is nil; in the other case, the argument is nil.

Now I've got a grumpy error message from Xcode telling me that I've sent a message to a nil object.

Incorrect. Your NSCalendar (the receiver) is not nil. What's nil is the NSDate you're passing as one of the arguments to the method. NSCalendar doesn't have any clue what to do when you give it a nil NSDate, so it gives you a log message instead of throwing an exception and crashing your app.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!