在数组完成后执行一个函数 - Swift

时间:2018-03-17 20:33:38

标签: swift

我想在完全设置数组后执行一个函数。我试图在数组的声明中实现didSet,但每次将一个元素添加到数组时,都会调用其中的代码。有什么办法可以做我想要的吗? 以下是我的一些代码:

var arrayScore = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] {
    didSet {
        for i in 0..<arrayScore.count {
            UserDefaults.standard.set(Int(arrayScore[i]), forKey: "arrayScore\(i)")
        }
        LineGraphView.setNeedsDisplay()
    }
}

2 个答案:

答案 0 :(得分:0)

一个解决方案是filter值为零的项目。如果结果数组为空,则所有值都为非零

if arrayScore.filter({ $0 == 0.0 }).isEmpty {
    print("every value in the array is > 0.0")
}

或 - 如Sulthan所述 - 使用contains

更有效率
if !arrayScore.contains(0.0) {
    print("every value in the array is > 0.0")
}

但是,如果您想检查项目的总和是否为零,请使用

if arrayScore.reduce(0.0, + ) != 0.0 {
    print("at least one value in the array is not zero")
}

答案 1 :(得分:0)

试试这个:

docker exec -it local-mysql /bin/bash -c "mysql -h localhost < /data/init.sql"
相关问题