大佬笔记
enum Country {
case mond
case liyue
case ricewife
case sumeru
}
var country = Country.mond
switch country {
case .mond:
print("蒙德")
case .liyue:
print("璃玥")
case .ricewife:
print("稻妻")
case .sumeru:
print("须弥")
}
enum Country:CaseIterable {
case mond
case liyue
case ricewife
case sumeru
}
Country.allCases
enum Country {
case attribute(String,String)
case demongod(String,String)
}
var country = Country.attribute("稻妻", "雷之国")
country = .demongod("枫丹", "芙宁娜")
switch country {
case .attribute(let countryName, let attribute):
print("\(countryName) - \(attribute)")
case .demongod(let countryName, let demonName):
print("\(countryName) 神明属性:\(demonName)")
}
enum AllError:Error {
case errorA
case errorB
case errorC
}
extension AllError:LocalizedError {
var errorDescription: String? {
switch self {
case .errorA:
return "第一个错误..."
case .errorB:
return "第二个错误..."
case .errorC:
return "第三个错误..."
}
}
}
extension AllError:CustomNSError {
var errorCode: Int {
return 404
}
}
func loginError(username:String?) throws {
if username == nil {
throw AllError.errorA
}
print("走下一步")
}
do {
try loginError(username: nil)
} catch {
let cError = error as? CustomNSError
debugPrint(cError?.errorCode ?? 0)
}
do {
try loginError(username: nil)
} catch {
debugPrint(error.localizedDescription)
}