发送NSCFCalendar nil NSDate讽刺错误 [英] Sending NSCFCalendar nil NSDate Sarcastic Error

查看:173
本文介绍了发送NSCFCalendar nil NSDate讽刺错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一些主题讨论将零日期发送到Calendar方法时控制台中弹出的新的iOS 6 Xcode错误:

A few threads here discuss the new iOS 6 Xcode error that pops up 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 error made me chuckle a bit, but got me to thinking....

我编写的导致此错误的代码从多个来源(日期格式化程序,字符串,用户输入等)中获取日期信息,而且我真的不希望所提供的每个日期都有效-我完全知道有些将为零,并且我很高兴处理此类对象的结果.

The code I've written that results in this error takes date information from a multitude of sources (from date formatters, strings, user input, etc), and I really don't expect every date that is supplied to be valid - I know full well that some will be nil, and I'm quite happy dealing with the results of such objects.

当我第一次开始在Objective C中编程时,我读过的功能之一是

When I first started programming in Objective C one of the features I read up on was sending messages to nil objects. This was great because it meant that I didn't have to worry about passing around nil objects.

现在,我收到了来自Xcode的脾气暴躁的错误消息,告诉我已经向nil对象发送了一条消息.

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

现在推断我必须在方法中使用每个对象之前检查每个对象是否为nil?我在用零对象做一些令人发指的事情吗?

Is the inference now that I've got to check every object is not nil before using it in methods? Am I doing something totally heinous working with nil objects?

推荐答案

现在推断我必须在方法中使用每个对象之前检查每个对象是否为nil?

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

不,那不是推论.推断是,您必须检查要发送给NSCalendar的日期参考,因为向其发送nil参考的结果是不确定的.目前,它可能对您有用,但是Apple可以在将来的版本中更改其行为,以执行所需的任何操作,甚至可以阻止使用婴儿密封.哦,您能不能想想海豹宝宝?

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?

不.如果您想要返回的结果为零或零,则将消息发送到可能为零的接收者是100%的罚款.在这种情况下,您不会向nil发送消息.您正在向没有期望的非零接收者发送零自变量.在一种情况下,接收者为零;在另一种情况下,参数为nil.

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.

这篇关于发送NSCFCalendar nil NSDate讽刺错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆