Val*_*adu 32 iphone cocoa-touch objective-c nsindexpath
我正在寻找一种方法来测试索引路径是否相等,并且相等我的意思是在每个级别上相等?我试过compare:
但似乎没有工作,相比之下,NSOrderedSame
即使索引绝对不一样,我也总是如此.
Ita*_*ber 90
几乎所有的Objective-C对象都可以使用该isEqual:
方法进行比较.所以,为了测试平等,你只需要[itemCategoryIndexPath isEqual:indexPath]
,而且你很高兴.现在,这可以NSObject
实现isEqual:
,因为所有对象都自动拥有该方法,但是如果某个类没有覆盖它,isEqual:
则只会比较对象指针.
在这种情况下NSIndexPath
,由于该isEqual:
方法已被覆盖,您可以按照预期比较对象.但是,如果我要编写一个新类,MyObject
而不是覆盖该方法,则[instanceOfMyObject isEqual:anotherInstanceOfMyObject]
实际上将是相同的instanceOfMyObject == anotherInstanceOfMyObject
.
您可以在NSObject协议参考中阅读更多内容.
在Swift中,您==
可以比较NSIndexPaths是否相同.
import UIKit
var indexPath1 = NSIndexPath(forRow: 1, inSection: 0)
var indexPath2 = NSIndexPath(forRow: 1, inSection: 0)
var indexPath3 = NSIndexPath(forRow: 2, inSection: 0)
var indexPath4 = indexPath1
println(indexPath1 == indexPath2) // prints "true"
println(indexPath1 == indexPath3) // prints "false"
println(indexPath1 == indexPath4) // prints "true"
println(indexPath1 === indexPath2) // prints "true"
println(indexPath1 === indexPath3) // prints "false"
println(indexPath1 === indexPath4) // prints "true"
Run Code Online (Sandbox Code Playgroud)
Swift ==
用于价值比较.===
用于检测两个变量何时引用完全相同的实例(内存中的位置等).有趣的是,indexPath1 === indexPath2
显示NSIndexPath被构建为在值(节行)匹配时共享相同的实例,因此即使您正在比较实例,它仍然有效.
这个答案几乎完全取自drewag的这个精彩的答案,并在这里转载,因为这是'比较indexpath'的第一个Google结果,我们不应该只是粘贴一个链接.
归档时间: |
|
查看次数: |
18623 次 |
最近记录: |