Swift 用do和catch替换防护罩
我有以下实现,我正在向ViewController提出问题以正确显示警报。正如您在我的代码中所看到的,我在一行中使用了guard let和try。我想这很管用,但不推荐 处理/重构代码以正确处理每个抛出案例的最佳方法是什么Swift 用do和catch替换防护罩,swift,Swift,我有以下实现,我正在向ViewController提出问题以正确显示警报。正如您在我的代码中所看到的,我在一行中使用了guard let和try。我想这很管用,但不推荐 处理/重构代码以正确处理每个抛出案例的最佳方法是什么 enum ServiceError: Error { case ClassroomApplicationCreation(Error) case ClassroomDoesNotExist(Error) case ClassRoomSelectionError }
enum ServiceError: Error {
case ClassroomApplicationCreation(Error)
case ClassroomDoesNotExist(Error)
case ClassRoomSelectionError
}
func createClassroom() throws -> String {
guard let selectedClass = try findClassroom() else {
throw ServiceError.ClassSelectionError
}
guard let classURL = URL(string: selectedClass) else {
throw ServiceError.ClassroomDoesNotExist
}
let authority = try Authority(url: classURL)
let configuration = ClassroomApplicationConfig(className: selectedClass, redirectUri: nil, authority: authority)
do {
return try ClassRoom(configuration: configuration)
} catch {
throw ServiceError.ClassroomApplicationCreation(error)
}
}
func findClassroom() throws -> String? {
guard
let selectedClass = UserDefaults.standard.string(forKey: selectedClass)
else {
throw ServiceError.ClassRoomSelectionError
}
do {
return try serviceManager.retrieveClassroom(selectedClass)
} catch {
throw ServiceError.ClassRoomSelectionError
}
}
假设教室(配置:配置)
在失败时抛出。classroomApplicationCreation
和findClassroom
抛出。classRoomSelectionError
并在成功时返回一个非可选的错误。代码可以减少为
enum ServiceError: Error {
case classroomApplicationCreation(Error)
case userDoesNotExist(Error)
case classRoomSelectionError
}
func createClassroom() throws -> ClassRoom {
let selectedClass = try findClassroom()
guard let classURL = URL(string: selectedClass) else {
throw ServiceError.userDoesNotExist
}
let authority = try Authority(url: classURL)
let configuration = ClassroomApplicationConfig(classId: selectedClass.ID, redirectUri: nil, authority: authority)
return try ClassRoom(configuration: configuration)
}
假设教室(配置:配置)
抛出。classroomApplicationCreation
和findClassroom
在失败时抛出。classRoomSelectionError
,在成功时返回非可选的代码,代码可以减少为
enum ServiceError: Error {
case classroomApplicationCreation(Error)
case userDoesNotExist(Error)
case classRoomSelectionError
}
func createClassroom() throws -> ClassRoom {
let selectedClass = try findClassroom()
guard let classURL = URL(string: selectedClass) else {
throw ServiceError.userDoesNotExist
}
let authority = try Authority(url: classURL)
let configuration = ClassroomApplicationConfig(classId: selectedClass.ID, redirectUri: nil, authority: authority)
return try ClassRoom(configuration: configuration)
}
返回类型应该是
教室
您可以重构,这样就没有任何防护或do/catch块:
func createClassroom() throws -> ClassRoom {
let selectedClass = try findClassroom()
let classURL = try makeClassURL(from: selectedClass)
let authority = try Authority(url: classURL)
let configuration = ClassroomApplicationConfig(classId: selectedClass.ID, redirectUri: nil, authority: authority)
return try makeClassRoom(from: configuration)
}
func findClassroom() throws -> String {
guard allIsWell else { throw ServiceError.ClassRoomSelectionError }
return ""
}
func makeClassURL(from string: String) throws -> URL {
guard let classURL = URL(string: string) else {
throw ServiceError.UserDoesNotExist
}
return classURL
}
func makeClassRoom(from configuration: ClassroomApplicationConfig) throws -> ClassRoom {
do {
return try ClassRoom(configuration: configuration)
} catch {
throw ServiceError.ClassroomApplicationCreation(error)
}
}
您可以重构,这样就没有任何防护或do/catch块:
func createClassroom() throws -> ClassRoom {
let selectedClass = try findClassroom()
let classURL = try makeClassURL(from: selectedClass)
let authority = try Authority(url: classURL)
let configuration = ClassroomApplicationConfig(classId: selectedClass.ID, redirectUri: nil, authority: authority)
return try makeClassRoom(from: configuration)
}
func findClassroom() throws -> String {
guard allIsWell else { throw ServiceError.ClassRoomSelectionError }
return ""
}
func makeClassURL(from string: String) throws -> URL {
guard let classURL = URL(string: string) else {
throw ServiceError.UserDoesNotExist
}
return classURL
}
func makeClassRoom(from configuration: ClassroomApplicationConfig) throws -> ClassRoom {
do {
return try ClassRoom(configuration: configuration)
} catch {
throw ServiceError.ClassroomApplicationCreation(error)
}
}
为什么可以沿着Authority init转发,而其他错误必须进行模糊处理?我想,如果你的方法抛出错误,你不应该在那里捕获错误。只需从findClassroom方法和课堂初始值设定项中抛出正确的错误。@LeoDabus,你能用一个例子详细说明一下吗?只需删除
do catch
,并从初始值设定项和findClassroom方法中抛出错误。你指的是最后一个do catch
?你指的是哪一个初始值设定项?为什么可以沿着Authority init转发,而其他错误必须被混淆?如果你的方法抛出,你不应该在那里捕获错误。只需从findClassroom方法和课堂初始值设定项中抛出正确的错误。@LeoDabus,你能用一个例子详细说明一下吗?只需删除do catch
,并从初始值设定项和findClassroom方法中抛出错误。你指的是最后一个do catch
?您指的是哪个初始值设定项?我有固定的findClassroom()
method我有固定的findClassroom()
method