Created
July 28, 2012 19:06
-
-
Save brentsimmons/3194441 to your computer and use it in GitHub Desktop.
Checking if an object is empty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BOOL RSIsEmpty(id obj) { | |
return obj == nil || obj == [NSNull null] || ([obj respondsToSelector:@selector(length)] && [(NSData *)obj length] == 0) || ([obj respondsToSelector:@selector(count)] && [obj count] == 0); | |
} | |
BOOL RSStringIsEmpty(NSString *s) { | |
/*22 Feb. 2011: added NSNull check. JSON parser can put a null where we expect a string, and NSNull throws an exception when checking length. Since [NSNull null] is, arguably, emptiness, it makes sense to include it.*/ | |
return s == nil || (id)s == (id)[NSNull null] || [s length] == 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In general I agree with you completely -- the object-oriented approach is best. But I will consider that there are occasional special cases, and I think this is one.