How can I tell if an NSMutableDictionary
is empty in Objective-C?
If you wish to check if the NSDictionary contains any key (non-specific) you should use [dictionary allKeys]. count == 0 If the count is 0 there are no keys in the NSDictionary .
To check if a Dictionary is empty in Swift, check isEmpty property of this Dictionary instance. isEmpty property returns a boolean value of true if the dictionary is empty, or false if the dictionary is not empty.
Sending a message to nil / NULL is always valid in Objective-C. So it depends if you want to check if a dictionary is nil , or if it doesn't have values (as a valid dictionary instance may be empty). In the first case, compare with nil . Otherwise, checks if count is 0, and ensure you're instance is still valid.
An object representing a dynamic collection of key-value pairs, for use instead of a Dictionary variable in cases that require reference semantics.
Very simple, use the -count
method inherited from NSDictionary
:
NSMutableDictionary *dict = ... BOOL isEmpty = ([dict count] == 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With