@
nobodyknows 感谢,可能是我描述不够清晰,我其实是要做 optional<subclass>与 optional<class>的继承关系比较,而不是==
protocol OptionalProtocol {
static func wrappedType() -> Any.Type
func wrappedType() -> Any.Type
}
extension Optional: OptionalProtocol {
static func wrappedType() -> Any.Type {
return Wrapped.self
}
func wrappedType() -> Any.Type {
return Wrapped.self
}
}
class KeyValueCoding {
init() {
defaultKeyValue()
}
func defaultKeyValue() {
// let userDefault = UserDefaults.standard
let classPath = String(describing: self)
let mirror = Mirror(reflecting: self)
for child in mirror.children {
guard let label = child.label else { continue }
let fullLabel = classPath + "." + label
let type = type(of: child.value)
let value = child.value
print("\(fullLabel) \(type)")
if let wrappedType = (type as? OptionalProtocol.Type)?.wrappedType() {
print("optional \(wrappedType)")
} else {
print("not optional")
}
}
}
}