2012-01-19 78 views
0

我,如果我做错了什么这个if语句woundering ...如果不indexpath工作正常声明

我compairing 2个indexPaths,老一个和当前选中的一个,如果他们不相等,我想做一些其他明智的继续,因为正常..

但是,如果统计总是被访问,即使他们是!= ..我做错了什么..我知道你可以检查IsEqual:是这里的东西对于!=?

继承人我的代码和输出

NSLog(@"%@", modelIndexPath); 
      NSLog(@"%@", oldCheckedIndexPath); 
      NSLog(@"%@", indexPath); 
      if (modelIndexPath != indexPath) { 
       NSLog(@"Not Equal"); 
       NSLog(@" "); 
      } 

2012-01-19 16:09:06.521 Code[9907:207] (null) 
2012-01-19 16:09:06.522 Code[9907:207] (null) 
2012-01-19 16:09:06.523 Code[9907:207] <NSIndexPath 0xc2993e0> 2 indexes [9, 0] 
2012-01-19 16:09:06.524 Code[9907:207] Not Equal 
2012-01-19 16:09:06.525 Code[9907:207] 
2012-01-19 16:09:17.921 Code[9907:207] <NSIndexPath 0xc2993e0> 2 indexes [9, 0] 
2012-01-19 16:09:17.922 Code[9907:207] <NSIndexPath 0xc2993e0> 2 indexes [9, 0] 
2012-01-19 16:09:17.923 Code[9907:207] <NSIndexPath 0xc5b0c50> 2 indexes [9, 0] 
2012-01-19 16:09:17.924 Code[9907:207] Not Equal 
2012-01-19 16:09:17.925 Code[9907:207] 
2012-01-19 16:09:29.489 Code[9907:207] <NSIndexPath 0xc5b0c50> 2 indexes [9, 0] 
2012-01-19 16:09:29.490 Code[9907:207] <NSIndexPath 0xc5b0c50> 2 indexes [9, 0] 
2012-01-19 16:09:29.491 Code[9907:207] <NSIndexPath 0xc7886c0> 2 indexes [9, 2] 
2012-01-19 16:09:29.491 Code[9907:207] Not Equal 
2012-01-19 16:09:29.492 Code[9907:207] 
2012-01-19 16:09:34.945 Code[9907:207] <NSIndexPath 0xc7886c0> 2 indexes [9, 2] 
2012-01-19 16:09:34.946 Code[9907:207] <NSIndexPath 0xc7886c0> 2 indexes [9, 2] 
2012-01-19 16:09:34.946 Code[9907:207] <NSIndexPath 0xc89e780> 2 indexes [9, 2] 
2012-01-19 16:09:34.947 Code[9907:207] Not Equal 
2012-01-19 16:09:34.948 Code[9907:207] 
2012-01-19 16:09:37.601 Code[9907:207] <NSIndexPath 0xc89e780> 2 indexes [9, 2] 
2012-01-19 16:09:37.602 Code[9907:207] <NSIndexPath 0xc89e780> 2 indexes [9, 2] 
2012-01-19 16:09:37.602 Code[9907:207] <NSIndexPath 0xc94e480> 2 indexes [9, 1] 
2012-01-19 16:09:37.603 Code[9907:207] Not Equal 
2012-01-19 16:09:37.604 Code[9907:207] 

更新:

这里是事务与你们

NSLog(@"%@", modelIndexPath); 
      NSLog(@"%@", oldCheckedIndexPath); 
      NSLog(@"%@", indexPath); 

      if ([modelIndexPath isEqual:indexPath]) NSLog(@"hurray!"); 
      if (![modelIndexPath isEqual:indexPath]) NSLog(@"bummer"); 
      NSLog(@" "); 



2012-01-19 16:41:54.853 Code[11071:207] (null) 
2012-01-19 16:41:54.854 Code[11071:207] (null) 
2012-01-19 16:41:54.855 Code[11071:207] <NSIndexPath 0xc21f9a0> 2 indexes [9, 0] 
2012-01-19 16:41:54.856 Code[11071:207] bummer 
2012-01-19 16:41:54.856 Code[11071:207] 
2012-01-19 16:41:59.637 Code[11071:207] <NSIndexPath 0xc21f9a0> 2 indexes [9, 0] 
2012-01-19 16:41:59.637 Code[11071:207] <NSIndexPath 0xc21f9a0> 2 indexes [9, 0] 
2012-01-19 16:41:59.638 Code[11071:207] <NSIndexPath 0x6a73e10> 2 indexes [9, 0] 
2012-01-19 16:41:59.639 Code[11071:207] hurray! 
2012-01-19 16:41:59.639 Code[11071:207] 
2012-01-19 16:42:05.492 Code[11071:207] <NSIndexPath 0x6a73e10> 2 indexes [9, 0] 
2012-01-19 16:42:05.493 Code[11071:207] <NSIndexPath 0x6a73e10> 2 indexes [9, 0] 
2012-01-19 16:42:05.494 Code[11071:207] <NSIndexPath 0x6aae9d0> 2 indexes [9, 0] 
2012-01-19 16:42:05.494 Code[11071:207] hurray! 
2012-01-19 16:42:05.495 Code[11071:207] 
2012-01-19 16:42:10.901 Code[11071:207] <NSIndexPath 0x6aae9d0> 2 indexes [9, 0] 
2012-01-19 16:42:10.902 Code[11071:207] <NSIndexPath 0x6aae9d0> 2 indexes [9, 0] 
2012-01-19 16:42:10.902 Code[11071:207] <NSIndexPath 0xc625470> 2 indexes [9, 2] 
2012-01-19 16:42:10.903 Code[11071:207] bummer 
+0

为了清楚起见,可以删除行打印出'oldCheckedIndexPath ',因为它与你的陈述无关。然后你会很快看到它现在正在工作。 – mvds

回答

2

那些NSIndexPath万物皆对象的帮助下,目前的状态,你正在比较他们的内存地址。换句话说,和indexPath都是指针。 (谷歌是你的朋友)

比较这些对象,使用方法:

if ([modelIndexPath isEqual:indexPath]) NSLog(@"hurray!"); 

if (![modelIndexPath isEqual:indexPath]) NSLog(@"bummer"); 

测试不平等。

+0

这意味着什么剂量,因为它似乎在正确的时间调用正确的if语句。什么是测试不平等? –

+0

@ C.Johns“检验不平等”与“检查不平等”是一样的。请使用'isEqual:'东西来显示你的代码+日志。 – mvds

+0

更新...抱歉花了这么长时间才清理东西 –

1

您正在比较实例地址。 使用if (![modelIndexPath isEqual:indexPath]) { //...

4

NSIndexPath为对象,采用比较它们

if (![modelIndexPath isEqual:indexPath]) 

编辑: 可能发布更多的代码,上面的代码应该做的罚款,或者你也可以做,(取决于您的需要无论如何)

int modelRow = [modelIndexPath row]; 
int indexRow = [indexPath row]; 

if (modelRow != indexRow) 
{ 
    // Do something here 
} 

请注意,这只会根据您的需要工作!的==

==

+0

我只是做了这个..它仍然这样做一些bizzar的原因.... –

+0

看到我的编辑,但该代码将取决于你想要做什么,可能不适合你的情况 –

1

使用isEqual:代替意味着如果两个参考点,以相同的对象

isEqual:装置到对象相等

if ([modelIndexPath isEqual: indexPath]) { 
       NSLog(@"Not Equal"); 
       NSLog(@" "); 
      }