【发布时间】:2018-01-06 12:38:25
【问题描述】:
我想编写一个接受任何类型的函数来确定它的工作。但我遇到了dictionary.type 的错误。它为其占位符的键请求特定类型。
有什么办法可以做到吗?
我可能会这样使用:
foo(type: type(of: someDict)) // someDict can be any type of dictionary.
// can be [String: Any] or [Int, Any] or [Double: Any] or [Character: Any], etc.
func foo(type: Any.Type) {
switch type {
case is Int.Type:
print("is Int")
//case is Dictionary: // error, Reference to generic type 'Dictionary' requires arguments in <...>
case is Dictionary<Hashable, Any>: // also error, Using 'Hashable' as a concrete type conforming to protocol 'Hashable' is not supported
print("is Dictionary")
default:
print("don't know")
}
}
【问题讨论】: