简体   繁体   English

为什么我的 for-in 循环不起作用? 错误处理 Swift

[英]Why do my for-in loop doesn't work? Error Handling Swift

I tried a error handling function to crack the code of a safe(for example), In my thoughts the for-in loop should print all the tries which are in the array... But the thing is that just the first String of the array gets printed and the others aren`t.我尝试了一个错误处理 function 来破解保险箱的代码(例如),在我看来,for-in 循环应该打印数组中的所有尝试......但问题是只是第一个字符串数组被打印出来,而其他的则没有。 Can anyone tell me where is my failure?谁能告诉我我的失败在哪里?

enum SafeError: Error {
    case wrongCode
    case tooMuchTries
    case safeIsOpen
}

 struct Safe {
    var safeIsclosed: Bool
    var code: String
    var maximumTries: Int
    mutating func OpenSafe(triedCodes: [String]) throws {
        guard safeIsclosed else {
            throw SafeError.safeIsOpen
        }
        for triedCode in triedCodes {
            print("Try \(triedCode)")
            
        
            guard triedCode == code else {
                throw SafeError.wrongCode
            }
        }
        guard triedCodes.count <= maximumTries else {
            throw SafeError.tooMuchTries
        }

    print("Password was correct")
        
    }
    
}
var safe = Safe(safeIsclosed: true, code: "WD1146", maximumTries: 5)

do {
    try safe.OpenSafe(triedCodes: ["DD377789", "123456", "WD1146", "112378AADD", "DDFG4567"])
} catch SafeError.safeIsOpen {
    print("Safe is already open")
} catch SafeError.tooMuchTries {
    print("Safe is closed now, your try was wrong")
} catch SafeError.wrongCode {
    print("Wrong Password")
} catch {
    print("Error")
}

Thanks in advance!提前致谢!

Calling throw from a function terminates the execution of said function immediately.从 function 调用throw会立即终止所述 function 的执行。 This means that as soon as the first element of triedCodes is wrong, your function terminates because a .wrongCode error is thrown.这意味着一旦尝试代码的第一个元素是错误的,您的triedCodes就会终止,因为抛出.wrongCode错误。

I would suggest modifying your function such that you check whether the correct code is contained in the triedCodes array and throw an error in case it isn't.我建议修改您的 function 以便检查是否正确的代码包含在triedCodes数组中,如果不是则抛出错误。 This ensures that the whole array is checked before an error is thrown.这可确保在引发错误之前检查整个数组。

mutating func openSafe(triedCodes: [String]) throws {
    guard safeIsclosed else {
        throw SafeError.safeIsOpen
    }

    guard triedCodes.contains(code) else {
        throw SafeError.wrongCode
    }

    guard triedCodes.count <= maximumTries else {
        throw SafeError.tooMuchTries
    }
}

Once you get the first exception you are out of the loop.一旦你得到第一个异常,你就脱离了循环。

Try changing the struct like this:尝试像这样更改结构:

struct Safe {
var safeIsclosed: Bool
var code: String
var maximumTries: Int

func OpenSafe(triedCode: String) throws {
    guard safeIsclosed else {
        throw SafeError.safeIsOpen
    }
    print("Try \(triedCode)")
    
    guard triedCode == code else {
        throw SafeError.wrongCode
    }
}

mutating func OpenSafe(triedCodes: [String]) throws {
    guard safeIsclosed else {
        throw SafeError.safeIsOpen
    }
    for triedCode in triedCodes {
        do {
            try safe.OpenSafe(triedCode: triedCode)
        } catch SafeError.safeIsOpen {
            print("Safe is already open")
        } catch SafeError.wrongCode {
            print("Wrong Password")
        } catch {
            print("Error")
        }
    }
    guard triedCodes.count <= maximumTries else {
        throw SafeError.tooMuchTries
    }

    print("Password was correct") 
}
}

I cannot test it right now, so I could have syntax errors.我现在无法测试它,所以我可能会遇到语法错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM