Hortitude
Hortitude

Reputation: 14058

Cannot set IndexPath row in Swift

The following code produces a runtime error and I can't figure out how. Any thoughts?

    var foo: IndexPath
    foo = IndexPath()
    foo.row = 1

    var i = 0

enter image description here

Upvotes: 1

Views: 477

Answers (1)

Jawad Ali
Jawad Ali

Reputation: 14397

There is a preCondition in swift to access row of indexpath

/// The section of this index path, when used with `UITableView`.
///
/// - precondition: The index path must have exactly two elements.
public var section: Int

/// The row of this index path, when used with `UITableView`.
///
/// - precondition: The index path must have exactly two elements.
public var row: Int

enter image description here

If you want to access or change row you need to initialise indexPath with row and section

 var foo: IndexPath
 foo = IndexPath(row: 0, section: 0)
 foo.row = 1 // return foo (1,0)

Upvotes: 1

Related Questions