我有引发错误的错误吗?我该怎么办?

问题描述 投票:0回答:1

我最近尝试运行此代码

extension String: Error {} //string isn't throw-able by default 

func throwingFunction() throws { //this function always throws on call. So far so good
    throw "error"
}

switch try throwingFunction() {
    case "error": print("It works!") //but it doesn't 
    default: break
}

错误为expression pattern of type 'String' cannot match values of type '()'这个函数不应该返回原始字符串吗?难道我做错了什么?请帮助。

swift error-handling switch-statement
1个回答
0
投票

throwingFunction()返回Void()。您无法打开Void

do {
    try throwingFunction()
} catch {
    switch error as! String {
    case "error": print("It works!")
    default: break
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.