【发布时间】:2016-11-13 18:09:15
【问题描述】:
我正在向我继承的 Web API 框架添加更好的错误处理。目前它做了一堆强制转换,当数据与预期不匹配时会导致崩溃。
我想将 as! 的所有用法替换为检查类型并在失败时抛出异常(带有详细说明)的函数。
这就是我到目前为止所做的:
func checkType<T>(type: AnyClass, value: T?, name: String) throws -> T {
guard let value = value else {
let message = "[\(name)] Expected \(type), but value was nil"
throw PlaidsterError.InvalidType(message)
}
guard value.dynamicType.self == type else {
let message = "[\(name)] Expected \(type), but it was an \(value.dynamicType.self) with value: \(value)"
throw PlaidsterError.InvalidType(message)
}
return value
}
但这有多个问题。它只能接受对象类型,如果类不完全匹配则失败(例如 NSString 失败,因为它实际上是一个 __NSCFString),并且它不能用于任何类型,即 String、Int、Double...
我似乎无法为Any 值类型找到AnyClass 的同义词,这是第一个问题。此外,似乎不可能做类似value.dynamicType.self is type 的事情,因为它说type 不是一种类型。
有可能做我想做的事吗?有没有更好的方法来进行这种类型检查,而不需要大量的样板代码分散在解析代码中?
我的目标是得到这样甚至更简单的东西:
public struct PlaidCategory {
// MARK: Properties
public let id: String
public let hierarchy: [String]
public let type: String
// MARK: Initialization
public init(category: [String: Any]) throws {
id = try checkType(String.self, value: category["id"], name: "id")
hierarchy = try checkType([String].self, value: category["hierarchy"], name: "hierarchy")
type = try checkType(String.self, value: category["type"], name: "type")
}
}
【问题讨论】:
-
我建议你看看 github 上众多的 swift json 解析器之一,看看你是否能找到一些有趣的“github swift json”。 SwiftyJSON、Gloss 等。
标签: swift generics introspection