代码之家  ›  专栏  ›  技术社区  ›  A O

我可以通过引用声明一个局部数组变量来减少重复的代码吗

  •  1
  • A O  · 技术社区  · 6 年前

    所以我知道快速阵列是 struct ,所以它们是按值传递的

    这是我的代码:

    firstSnapshotList secondSnapshotList 两者都是 [Snapshot]

        if datePicker == firstDatePicker {
            firstSnapshotList.removeAll()
    
            if let snapshots = SnapshotList.snapshotDict[dateKey] {
                for snapshot in snapshots {
                    firstSnapshotList.append(snapshot)
                }
            }
        } else if datePicker == secondDatePicker {
            secondSnapshotList.removeAll()
    
            if let snapshots = SnapshotList.snapshotDict[dateKey] {
                for snapshot in snapshots {
                    secondSnapshotList.append(snapshot)
                }
            }
        }
    

    如你所见,两个街区 if 语句是相同的,除了正在修改的数组。

    我想先声明一个变量, var snapshotList = *snapshot reference* 我是说, 如果我能把这个引用设置为 第一个快照列表 第二快照列表 根据日期选择器的不同,我的代码就是

    if datePicker == firstDatePicker {
        snapshotList = firstSnapshotList
    } else if datePicker == secondDatePicker {
        snapshotList = secondSnapshotList
    }
    
    snapshotList.removeAll()
    
    if let snapshots = SnapshotList.snapshotDict[dateKey] {
        for snapshot in snapshots {
            snapshotList.append(snapshot)
        }
    }
    

    你可以说行数是一样的,所以它实际上不再可读或可维护了——这只是我个人的喜好,我认为第二个版本更有意义,主要是因为没有任何重复的代码

    但如果这只是快速生活方式的一部分,我也可以忍受。只是想知道是否可以将变量声明为引用

    3 回复  |  直到 6 年前
        1
  •  1
  •   Mihai Fratu    6 年前

    正如您所说,swift数组是结构,它们确实是作为值传递的。相反,可以将数组作为 inout 参数:

    func yourMethodName(_ array: inout [Snapshot]) {
        array.removeAll()
    
        if let snapshots = SnapshotList.snapshotDict[dateKey] {
            for snapshot in snapshots {
                array.append(snapshot)
            }
        }
    }
    

    然后在需要的地方调用方法,如下所示:

    if datePicker == firstDatePicker {
        yourMethodName(&firstSnapshotList)
    } else if datePicker == secondDatePicker {
        yourMethodName(&secondSnapshotList)
    }
    
        2
  •  1
  •   Modo Ltunzher    6 年前

    数组与标准库中的所有可变大小集合一样,使用写时复制优化。阵列的多个副本共享同一存储,直到您修改其中一个副本。当发生这种情况时,被修改的阵列将其存储替换为其自身唯一拥有的副本,然后在适当的位置进行修改。有时会应用可以减少复制量的优化。

        3
  •  1
  •   Alexander    6 年前

    您的代码可以简化到不需要减少代码重复的程度:

    let newSnapshotList = SnapshotList.snapshotDict[dateKey] ?? []
    
    if datePicker == firstDatePicker {
        firstSnapshotList = newSnapshotList
    } else if datePicker == secondDatePicker {
        secondSnapshotList = newSnapshotList
    }
    

    但是为了回答这个问题,这可以通过在现有函数中声明嵌套函数来完成:

    func overrideFromSnapshotDict(destination: inout [YourType]) {
        destination.removeAll()
        if let snapshots = SnapshotList.snapshotDict[dateKey] {
            for snapshot in snapshots {
                destination.append(snapshot)
            }
        }
    }
    
    if datePicker == firstDatePicker {
        overrideFromSnapshotDict(destination: &firstSnapshotList)
    } else if datePicker == secondDatePicker {
        overrideFromSnapshotDict(destination: &secondSnapshotList)
    }