首页 文章

Swift可变数组参数

提问于
浏览
0

所以我基本上试图改变元组变量的参数,我的代码如下所示:

var tabsections: [(sectionTitle: String?, rows: [String])]
tabsections = [("2017", []),
               ("2018", [])]

var mutableMonths = ["2017-02", "2017-10", "2018-01"]

for section in tabsections {
    for month in mutableMonths {
        if section.sectionTitle == month.split(separator: "-").first?.string {
            section.rows.append((month.split(separator: "-").last?.string)!)  // "Cannot use mutating member on immutable value of type '[String]'"
            mutableMonths.removeFirst()
        }
    }
}

但我一直在收到错误

不能在类型'[String]'的不可变值上使用变异成员

然后在指定更多 section 变量,同时通过 (sectionTitle, rows) 替换它来声明循环后,错误变为类似:

不能在不可变值上使用变异成员:'rows'是'let'常量

我找到了关于可变性的答案,例如这个,但仅针对函数(添加关键字 inout 会修复它)但作为局部变量,我的想法已经用完了 .

我在这里错过了一些小事吗?

任何帮助将不胜感激...

EDIT:

我忘了提到我知道一个基于C的循环可以解决这个问题

var i = 0
while i < tabsections.count {
    for month in mutableMonths {
        if tabsections[i].sectionTitle == month.split(separator: "-").first?.string {
            tabsections[i].rows.append((month.split(separator: "-").last?.string)!)
            mutableMonths.removeFirst()
        }
    }
    i += 1
}

但是我想知道在Swift 4中是否有更优雅/更好的方式这样做,因为在Swift 3中添加 var 就可以了 .

谢谢!

3 回答

  • 0

    你是对的 . 它可变 . 但是当你迭代 tabsections 时,for循环中的 section 无法改变 . 如果你写的东西(在for循环之前)

    tabsections[0].rows.append("2017-02")
    

    它会工作 .

    但是在for循环中尝试更改迭代的数组成员可能会导致问题 . 想想你是否正在开发编程语言 . 它有助于学习 . 如果有人在数组上进行迭代并从同一个数组中删除一个元素 . 程序状态如何?这就像切割你自己所坐的树枝 . :)

  • 0

    sectionlet 属性,属于 for 循环范围的本地属性 . 您可以尝试使用 for var section in ... ,但您仍然会制作本地副本(数组是值类型),这需要您将其重新分配回 tabsections 的正确索引,以便更改其中的数据 .

    相反,我建议你采用更实用的方法:

    func parse(dateString: String) -> (year: Int, month: Int) {
        let parts = dateString.split(separator: "-")
        return (year: Int(parts[0])!, month: Int(parts[1])!)
    }
    
    var mutableMonths = ["2017-02", "2017-10", "2018-01"]
    let yearMonthPairs = mutableMonths.map(parse(dateString:))
    
    let tabSections = Dictionary(grouping: yearMonthPairs, by: { $0.year })
        .mapValues{ $0.map{ $0.month } }
    

    这会生成一个类型为 [Int: [Int]] 的字典,这些字典在这些年内映射到数年到数月,全部为 Int ,因此它们更容易使用 . 当你需要将它显示为一个表时,你可以在它上面调用 .map{ year, months in (section: String(year), row: months.map(String.init)) } ,它将产生你的初始代码使用的相同元组数组

  • 0

    这个错误背后的原因是,在迭代你的第一个for循环时,'section'变量是let type.So,解决方案是:

    var tabsections: [(sectionTitle: String?, rows: [String])] = []
    tabsections = [("2017", []),
               ("2018", [])]
    
    var mutableMonths = ["2017-02", "2017-10", "2018-01"]
    
    for (index,section) in tabsections.enumerated() {
    for month in mutableMonths {
    if section.sectionTitle == month.split(separator: "-").first?.string {
            tabsections[index].rows.append((month.split(separator: "-").last?.string)!)  // "getting error as section is let type variable"
            mutableMonths.removeFirst()
      }
     }
    }
    

相关问题