ios - 在NSNotificationCenter中的通知的userInfo中传递空的NSIndexPath

标签 ios objective-c nsnotificationcenter nsindexpath userinfo

我有一个特定的情况,当订单列表(NSMutableArray)为空,因此所选索引(NSIndexPath)也应该为空(我在使用nil)。我正在使用NSNotificationCenter传递这些数据以触发应用程序其他位置的表中的更新。

但是,我收到一个错误:

*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* -[__ NSPlaceholderDictionary
initWithObjects:forKeys:count:]:尝试从中插入nil对象
对象[1]

如何通过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
                                                             }
 ];

最佳答案

您可以传递带有负值的NSIndexPath,并在接收方检查该值是否为负。

代码:

NSIndexPath *selectedIndex = [NSIndexPath indexPathForRow:-1 inSection:-1];

关于ios - 在NSNotificationCenter中的通知的userInfo中传递空的NSIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50871681/

相关文章:

swift - 在 Swift 2 中删除通知观察者的正确位置在哪里?

ios - 通过 NSDate 在 Realm 中过滤查询抛出 NSInvalidArgumentException

ios - 签署 iOS 应用程序 : App ID Cannot be Registered to Your Development Team

objective-c - 如何在 Objective-C 中添加观察者并在 Swift 3 中触发通知

objective-c - NSFileManager copyItemAtPath 提示存在一个不存在的文件

iphone - loadView 和 viewDidLoad 有什么区别?

ios - 如何在 NSNotification 类中选择一个选择器?

ios - 通过按按钮更改 UIWebView 中的文本。 (简单的?)

ios - 无法获取 Facebook 用户信息(生日、工作、教育程度)

iphone - 如何将 UIApplicationDelegate 添加到 UIResponder 链的末尾?