如何在函数外部更改字典中的Double值?

时间:2016-07-11 02:11:47

标签: ios swift function dictionary swift2

var grades: [String : Double]

grades = ["A": 0.0, "A-": 0.0, "B+": 0.0, "B": 0.0, "B-": 0.0, "C+": 0.0, "C": 0.0, "C-": 0.0, "D+": 0.0, "D": 0.0, "D-": 0.0, "F": 0.0]

func calcGPA() {

if let a = grades["A"], amin = grades["A-"], bplu = grades["B+"], b = grades["B"], bmin = grades["B-"], cplu = grades["C+"], c = grades["C"], cmin = grades["C-"], dplu = grades["D+"], d = grades["D"],    dmin = grades["D-"], f = grades["F"] {

        // Divide by this
        let gradesAdded = a + amin + bplu + b + bmin + cplu + c + cmin + dplu + d + dmin + f
        //grades multiplied by their values and added ex. a * 4.0 + amin * 3.7
        let gradesCalculated = a * 4.0 + amin * 3.7 + bplu * 3.3 + b * 3.0 + bmin * 2.7 + cplu * 2.3 + c *  2.0 + cmin * 1.7 + dplu * 1.3 + d * 1.0 + dmin * 0.7 // Dont do F because it would just add 0
        var gpa = gradesCalculated / gradesAdded

        if gpa.isNaN {
            gpa = 0.0
        }
    }
}

有没有办法做一些像成绩[“A”] + = 1.0的东西,以便它上升一个,我可以调用calcGPA()?我无法弄清楚如何使这项工作。对此的任何帮助都很棒

1 个答案:

答案 0 :(得分:2)

您可以通过强制解包查找来增加字典中的值:

grades["A"]! += 1.0

但这很危险,因为如果密钥不在字典中,它会崩溃。所以,你应该检查:

if let count = grades["A"] {
    grades["A"] = count + 1
}

以下是您的计划的更新版本:

func calcGPA(_ termGrades: [String]) -> Double? {

    var grades: [String: Double] = ["A": 0.0, "A-": 0.0, "B+": 0.0, "B": 0.0, "B-": 0.0, "C+": 0.0, "C": 0.0, "C-": 0.0, "D+": 0.0, "D": 0.0, "D-": 0.0, "F": 0.0]

    var gpa: Double?

    for grade in termGrades {
        if let count = grades[grade] {
            grades[grade] = count + 1
        } else {
            print("Hmmm, \(grade) is not a valid value for a grade")
            return nil
        }
    }

    if let a = grades["A"], amin = grades["A-"], bplu = grades["B+"], b = grades["B"], bmin = grades["B-"], cplu = grades["C+"], c = grades["C"], cmin = grades["C-"], dplu = grades["D+"], d = grades["D"], dmin = grades["D-"], f = grades["F"] {

        // Divide by this
        let gradesAdded = a + amin + bplu + b + bmin + cplu + c + cmin + dplu + d + dmin + f
        //grades multiplied by their values and added ex. a * 4.0 + amin * 3.7
        let gradesCalculated = a * 4.0 + amin * 3.7 + bplu * 3.3 + b * 3.0 + bmin * 2.7 + cplu * 2.3 + c *  2.0 + cmin * 1.7 + dplu * 1.3 + d * 1.0 + dmin * 0.7 // Dont do F because it would just add 0

        gpa = gradesAdded == 0 ? nil : gradesCalculated / gradesAdded
    }
    return gpa
}

// example calls
calcGPA(["E"])             // nil  "Hmmm, E is not a valid value for a grade"
calcGPA(["A-"])            // 3.7
calcGPA(["A", "B"])        // 3.5
calcGPA(["B", "B+", "A-"]) // 3.333333333333333
calcGPA([])                // nil

注意:

  1. 我更改了您的功能以获得一系列成绩并返回Double?(可选Double)。
  2. 如果出现任何问题,则返回
  3. nil(输入数组为空,输入数组包含无效等级,如“E”)。
  4. 通过在计算前检查除数,提前避免除以
  5. 替代解决方案:

    现在有一些完全不同的东西......

    受@ CodeBender评论的启发,这是一个使用带有关联值的enum来表示成绩的实现:

    enum Grade: Double {
        case A = 4.0
        case Aminus = 3.7
        case Bplus = 3.3
        case B = 3.0
        case Bminus = 2.7
        case Cplus = 2.3
        case C = 2.0
        case Cminus = 1.7
        case Dplus = 1.3
        case D = 1.0
        case Dminus = 0.7
        case F = 0
    }
    
    func calcGPA(_ termGrades: [Grade]) -> Double? {
        if termGrades.count == 0 {
            return nil
        } else {
            let total = termGrades.reduce(0.0) { (total, grade) in total + grade.rawValue }
            return total / Double(termGrades.count)
        }
    }
    
    // example calls
    calcGPA([.A, .B])              // 3.5
    calcGPA([.B, .Bplus, .Aminus]) // 3.3333333333
    calcGPA([.A, .A, .Bplus])      // 3.7666666666
    calcGPA([.F, .F, .F])          // 0
    calcGPA([])                    // nil