Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing NSIndexPath Swift

If I have a NSIndexPath constant declared for a UITableView, is it valid to compare using the == operator?

This is my constant declaration:

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

And then my function:

 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
}

This certainly works properly, but is it safe to do? I'm assuming that since it works, the == operator on the NSIndexPath object is comparing the section and row properties instead of the instance.

like image 240
Shan Avatar asked Aug 07 '14 00:08

Shan


People also ask

How does Swift compare to IndexPath?

The Swift overlay to the Foundation framework provides the IndexPath structure, which bridges to the NSIndexPath class. This means that, as an alternative to NSIndexPath , starting with Swift 3 and Xcode 8, you can use IndexPath . Note that IndexPath also conforms to Equatable protocol. Therefore, you can use == or !=

What is NSIndexPath in Swift?

Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.

What is IndexPath Objective C?

Index path is generally a set of two values representing row and section of a table view. Index path can be created in objective C as well as Swift as both are native language of iOS Development. IndexPathForRow is a class method in iOS.


2 Answers

Let's do a very simple test:

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"

Yes, it is safe to use == with NSIndexPath

As a side note, == in Swift is always for value comparisons. === is used for detecting when two variables reference the exact same instance. Interestingly, the indexPath1 === indexPath2 shows that NSIndexPath is built to share the same instance whenever the values match, so even if you were comparing instances, it would still be valid.

like image 131
drewag Avatar answered Oct 19 '22 03:10

drewag


With Swift, you can use NSIndexPath or IndexPath. Both have the same strategy to compare.


#1. NSIndexPath

According to Apple Documentation, NSIndexPath conforms to Equatable protocol. Therefore, you can use == or != operators in order to compare two instances of NSIndexPath.


#2. IndexPath

Apple documentation states about NSIndexPath and IndexPath:

The Swift overlay to the Foundation framework provides the IndexPath structure, which bridges to the NSIndexPath class.

This means that, as an alternative to NSIndexPath, starting with Swift 3 and Xcode 8, you can use IndexPath. Note that IndexPath also conforms to Equatable protocol. Therefore, you can use == or != operators in order to compare two instances of it.

like image 42
Imanou Petit Avatar answered Oct 19 '22 05:10

Imanou Petit