加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

比较NSIndexPath Swift

发布时间:2020-12-14 05:53:19 所属栏目:百科 来源:网络整理
导读:如果我为UITableView声明了一个NSIndexPath常量,使用==运算符进行比较是否有效? 这是我一贯的声明: let DepartureDatePickerIndexPath = NSIndexPath(forRow: 2,inSection: 0) 然后我的功能: override func tableView(tableView: UITableView!,heightFor
如果我为UITableView声明了一个NSIndexPath常量,使用==运算符进行比较是否有效?

这是我一贯的声明:

let DepartureDatePickerIndexPath = NSIndexPath(forRow: 2,inSection: 0)

然后我的功能:

override func tableView(tableView: UITableView!,heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
    var height: CGFloat = 45

    if indexPath == DepartureDatePickerIndexPath{
        height = departureDatePickerShowing ? 162 : 0
    } else if indexPath == ArrivalDatePickerIndexPath {
        height = arrivalDatePickerShowing ? 162 : 0
    }

    return height
}

这当然可以正常工作,但是安全吗?我假设,因为它的工作,NSIndexPath对象上的==运算符是比较部分和行属性而不是实例。

我们来做一个非常简单的测试:
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"

是的,可以安全地使用==与NSIndexPath

作为一个附注,在Swift中的==总是值得比较。 ===用于检测何时两个变量引用完全相同的实例。有趣的是,indexPath1 === indexPath2显示,NSIndexPath被构建为在值匹配时共享同一个实例,所以即使你在比较实例,它仍然是有效的。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读