-5

I need help.

I want to show data if there is data in dictionary else I want to show No-Data screen if there is no value in dictionary. But problem is like this that I'm getting count as 1. Though there is no value in dictionary.

As you all are trying to guess this one is duplicate. But my question is little-bit different. In this dictionary I have multiple key-value pairs. So I can't find for one key, that's why I'm just trying to get count of it's.

 NSLog(@"%lu",(unsigned long)dict.count); //<-- It prints : 1

 NSLog(@"%@", dict); //<----It will print below structure with no data

//Output :- 
    (
            {
        }
    )

So how do I will identify like this dictionary to show it is empty. Though I tried but it is going count as 1.

1 Answers1

0

According to the output dict is not a dictionary ({}), it's an array (()) containing one empty dictionary.

To check if there is a dictionary in the array which is not empty you could use (I renamed dict to array)

if (array.count > 0 && array[0].count > 0) { // will be evaluated to true if the dictionary is not empty
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Property 'count' not found on object of type 'id' -> array[0] –  Aug 02 '18 at 07:59
  • NSMutableArray *dict = self.product.productAllergenInformationModuleArray; // Assigning NSDictionary to it. –  Aug 02 '18 at 08:00
  • Declare the array `NSMutableArray *array = ` and please don't name it `dict` – vadian Aug 02 '18 at 08:02
  • if (array.count > 0 && [array[0] count] > 0) { NSLog(@"Null"); } else{ NSLog(@"%@", array); } –  Aug 02 '18 at 08:15
  • I've tried this way, as you told. But still it prints empty array instead of Null –  Aug 02 '18 at 08:16
  • 2
    Please **read** the code and try to understand it. The `if` block is printed if the array is **not** empty (`> 0`) and the `else` block if it's empty. Or check `if (array.count > 0 && array[0].count == 0) {` – vadian Aug 02 '18 at 08:20