无法在 Swift 中设置 IndexPath 行

Cannot set IndexPath row in Swift

以下代码会产生运行时错误,我不知道是如何发生的。有什么想法吗?

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

    var i = 0

swift 中有一个前提条件可以访问 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

如果您想访问或更改行,您需要使用行和部分初始化 indexPath

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