iosobjective-cnsnotificationcenternsindexpathuserinfo

Passing empty NSIndexPath in userInfo of a notification in NSNotificationCenter


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
                                                             }
 ];

Solution

  • 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];