|
|
|
|
|
by to3m
4913 days ago
|
|
A container might have an isEmpty function, and if the pointer is nil, that is probably more empty than not empty (though personally I don't believe either answer makes sense). "[x isEmpty]" might be more appropriate than "[x count]==0" for a list-type container. I always check for nil explicitly, personally. It sidesteps any question of whether the method makes sense in the nil case. And sending messages to nil can't work in general, because the return value could be a struct. |
|
I agree there isn't really a sensible answer for [nil isEmpty].
There are two uses of isEmpty I can think of.
We want to add an item to an empty container. If it's nil, we don't add an item to it; that seems okay. We check there will be an item before we get a value out of it. This one would cause issues.NSArray doesn't have an isEmpty method. I wonder if this is one of the reasons why, or if it's just something like wanting to keep the interface small.
[Edit] You can of course invert the logic. Using [container hasItems] in the two examples above would function just fine. While isEmpty seems to be the variant used everywhere, hasItems doesn't seem too unnatural.