1

I have a specific scenario, when an order list (NSMutableArray) is empty and therefore the selected index (NSIndexPath) should also be empty (I'm using nil). I am passing those data using NSNotificationCenter to trigger an update in a table elsewhere in the app.

However, I am receiving an error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'

How can I pass an empty index via NSNotificationCenter?

NSMutableArray *filteredAndSortedOrders = [[NSMutableArray alloc] init];
NSIndexPath *selectedIndex = nil;//[NSIndexPath indexPathForRow:0 inSection:0]]//The problem with this is that it's non-empty
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RELOAD_TABLE_AND_DATA
                                                    object:self
                                                  userInfo:@{
                                                             @"filteredAndSortedOrders":[filteredandSortedOrders copy],
                                                             @"selectedIndex":selectedIndex
                                                             }
 ];
Peter G.
  • 7,816
  • 20
  • 80
  • 154
  • You should check if `selectedIndex` == nil, your userInfo has only `@"filteredAndSortedOrders":[self.filteredandSortedOrders copy]` – Quoc Nguyen Jun 15 '18 at 08:24
  • Yes, `selectedIndex`==nil and that is causing the error. But I am looking for a proper way of passing empty NSIndexPath via notification. – Peter G. Jun 15 '18 at 08:25
  • Why you need to put the selectedIndex to your info. You can check in thê observer if has not object for `selectedIndex` key instead of – Quoc Nguyen Jun 15 '18 at 08:32

1 Answers1

2

You can pass NSIndexPath with with a negative value and check on receiver side that the value is negative.

Code:

NSIndexPath *selectedIndex = [NSIndexPath indexPathForRow:-1 inSection:-1];
Ivan Smetanin
  • 1,999
  • 2
  • 21
  • 28