【问题标题】:Passing empty NSIndexPath in userInfo of a notification in NSNotificationCenter在 NSNotificationCenter 的通知的 userInfo 中传递空的 NSIndexPath
【发布时间】:2018-06-15 08:21:37
【问题描述】:

我有一个特定的场景,当订单列表 (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
                                                             }
 ];

【问题讨论】:

  • 你应该检查selectedIndex == nil,你的userInfo只有@"filteredAndSortedOrders":[self.filteredandSortedOrders copy]
  • 是的,selectedIndex==nil,这就是导致错误的原因。但我正在寻找一种通过通知传递空 NSIndexPath 的正确方法。
  • 为什么需要将 selectedIndex 放入您的信息中。如果没有反对 selectedIndex 键而不是,您可以签入观察者

标签: ios objective-c nsnotificationcenter nsindexpath userinfo


【解决方案1】:

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

代码:

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

【讨论】:

  • 击败我 :)
猜你喜欢
  • 2015-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多