Checking if NSDate is valid before usage
In my code, I have a UIDatePicker that I use to create new NSDate objects, as well as modify some older ones. One of the things I'd like this UIDatePicker to do would be to load t开发者_开发问答he value of an existing NSDate when you choose to modify it. However, doing so when the existing NSDate* doesn't point to a valid date will cause a crash. Is there some way to check whether the pointer references a valid date before loading it? Thanks for your help.
No, that's not really possible. What you're basically asking is "can I tell whether a pointer to some location in memory points to an active, valid Objective-C object?" and you can't know. What you should do is ensure that your picker only ever sees valid values, by discarding the invalid ones as they become useless. E.g. when some code releases a date instance, set the pointer that code is using to nil
so it can't erroneously try to use the stale object pointer again.
精彩评论