编辑代码

func dayOfYear(year : Int, month : Int, day : Int) -> Int{
    var isRunNian : Bool!
    if year % 400 == 0 || (year % 4 == 0 && year % 100 != 0){
        isRunNian = true
    } else {
        isRunNian = false
    }
    var ret = day
    for i in 1 ..< month {
        switch i {
            case 1,3,5,7,8,10,12:ret += 31
            case 4,6,9,11:ret += 30
            default : if isRunNian {
                ret += 29
            } else {
                ret += 28
            }
        }
    }
    return ret
}

print(dayOfYear(year: 2021,month: 7, day: 12))