【问题标题】:Swift fatal error: Can't form Range with upperBound < lowerBoundSwift 致命错误:无法使用 upperBound < lowerBound 形成 Range
【发布时间】:2017-11-26 16:14:16
【问题描述】:

我有一个类,我想用 for 循环在那里增加价值。

这是我的课:

public class Expandable {
    public var name: String
    public var id:Int

    init(name: String, id:Int) {
        self.name = name
        self.id = id
    }

我可以这样手动添加:

let a = [Expandable(name:"aaa", id: 12)]

但是当我尝试使用 for 循环时,它根本不起作用。

for allin in items{
    sidebarMenuItem?.append(Expandable(name: allin["Code"] as! String, id: allin["Id"] as! Int))
}

Items 数据运行良好,但当我打印时 sidebarMenuItem 正在获得 Optional(MyProjectName.Expandable) 值。

更新:

我意识到我可以很好地获取数据,但我在expandCell 函数中遇到错误:

致命错误:无法使用 upperBound 形成 Range

我的 expandCell 函数:

private func expandCell(tableView: UITableView, index: Int) {
    // Expand Cell (add ExpansionCells
    if let expansions = sideBarData?[index]?.expandable {
        for i in 1...expansions.count {
            sideBarData?.insert(nil, at: index + i)
            tableView.insertRows(at: [NSIndexPath(row: index + i, section: 0) as IndexPath] , with: .top)
        }
    }
}

【问题讨论】:

  • sidebarMenuItem 显然是可选的(或者甚至是[Expandable?] 类型),您期望什么?在许多情况下,将数组声明为可选是没有意义的。
  • @vadian 我做了可选的,因为我在可扩展的 tableview 列表中使用了这个类。如果它是空的,我不会添加新的可扩展列表。这是我的目标。
  • 不要将数据源数组声明为可选并且不要在数据源数组中使用可选类型。还是底层表视图也是可选的?
  • 不,tableview 不是可选的。好的,我会解决的。那么你对我的问题有什么建议?
  • 试试for i in expansions.indices {

标签: ios swift class for-in-loop


【解决方案1】:

SWIIFT 4

要形成从上界到下界的范围,请使用stride,如本文档页面所示: Generic Function stride(from:to:by:)

for i in stride(from: 10, through: 5, by: -1) { print(i) }

如果您想包含下界,请大步前进: Generic Function stride(from:through:by:)

【讨论】:

    【解决方案2】:
    // swift 3.0
    let k = 5
    for i in stride(from: 10, through: k, by: 1) { print("i=\(i)") }
    for i in stride(from: 10, to: k, by: 1) { print("i=\(i)") }
    

    【讨论】:

    • 稍微解释一下你的代码,方便以后的读者阅读。
    【解决方案3】:

    试试这个:

      private func expandCell(tableView: UITableView, index: Int) {
            // Expand Cell (add ExpansionCells
            if let expansions = sideBarData?[index]?.expandable {
                //0 because your first item starts at 0
                for i in 0...expansions.count {
                    sideBarData?.insert(nil, at: index + i)
                    tableView.insertRows(at: [NSIndexPath(row: index + i, section: 0) as IndexPath] , with: .top)
                }
            }
        }
    

    【讨论】:

      【解决方案4】:

      我不确定您的扩展结构如何。但我猜测某些单元格可能是您的扩展计数为 0。这就是为什么当您的起始索引为 1 且结束索引为 0 时它会给出此错误。

      【讨论】:

        【解决方案5】:

        sidebarMenuItem 是可选的,因为您已将其声明为可选。试试这个。

        var sidebarMenuItem = [Expandable]()
        
        for allin in items{
            sidebarMenuItem.append(Expandable(name: allin["Code"] as! String, id: allin["Id"] as! Int))
        }
        

        【讨论】:

        • 它也不起作用。现在我收到致命错误:无法使用 upperBound
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-26
        • 1970-01-01
        • 2017-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多