这将是一个非常愚蠢的东西,但我的cellForRowAtIndexPath中有这个代码:
if ([self.indexPathSelected compare:indexPath] == NSOrderedSame) { NSLog(@" %d %d %d %d",self.indexPathSelected.row,self.indexPathSelected.section,indexPath.row,indexPath.section); }
打印:
0 0 0 0
0 0 1 0
0 0 2 0
0 0 3 0
0 0 4 0
0 0 5 0
我期待它只打印0 0 0 0.
我在这里做错了什么?
解决方法
由于您将indexPathSelected设置为nil,因此您需要在进行比较之前确保它不为零.
if (self.indexPathSelected && [self.indexPathSelected compare:indexPath] == NSOrderedSame) { NSLog(@" %d %d %d %d",indexPath.section); }
根据NSIndexPath’s compare method的文件:
Parameters
indexPath
Index path to compare.
This value must not be nil. If the value is nil,the behavior is undefined.