[英]If statement not working with indexpath properly
如果我对if语句做错什么,我会很痛苦。
我正在对两个indexPath进行配对,如果旧索引与当前选择的索引不相等,我想做其他一些明智的做法,如往常一样。
但是即使我的if语句总是被访问,即使它们是!= ..我做错了..我知道您可以检查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]
UPDate:
这是你们的帮助下的当前状况
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
NSIndexPath是对象,使用进行比较
if (![modelIndexPath isEqual:indexPath])
编辑:也许发布更多代码,上面的代码应该可以用,或者您也可以用,(仍然取决于您的需要)
int modelRow = [modelIndexPath row];
int indexRow = [indexPath row];
if (modelRow != indexRow)
{
// Do something here
}
请注意,这只会根据您的需要起作用!
这些NSIndexPath
事物是对象,您正在比较它们的内存地址。 换句话说, modelIndexPath
和indexPath
都是指针 。 (谷歌是你的朋友)
要比较这些对象,请使用:
if ( [modelIndexPath isEqual:indexPath] ) NSLog(@"hurray!");
要么
if ( ![modelIndexPath isEqual:indexPath] ) NSLog(@"bummer");
测试不平等。
您正在比较实例地址。 使用if (![modelIndexPath isEqual:indexPath]) { //...
使用isEqual:
而不是==
==
表示两个参考点是否指向同一对象
isEqual:
表示对象相等
if ([modelIndexPath isEqual: indexPath]) {
NSLog(@"Not Equal");
NSLog(@" ");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.