I'm trying to compare two Any.Type
.
What I want to check is whether some type is equal OR inherits from that type.
Eg. UIView
type is UIView.Type
but it also inherits from NSObject.Type
.
I'm having problems to check the second one.
Is it possible with Swift?
Exmaple:
class Object: NSObject {
var name: String = "aloha"
}
class Other {
var obj: Object?
}
let key = \Other.obj
let keyType = type(of: key)
keyType.valueType == Object?.self // true
keyType.valueType == NSObject.self // false - how can I make it true?
Edit 1:
My final goal is to have a generic VC to edit any (CoreData) object. I want to display different cells based on var's types. I find a different way for achieving what I described above (I hope - the better way). I'm still strugling though with one issue here. Although I can finally find out object's type, I want to check if that type conforms to my protocol, and if yes - call its methods. Sample code will probably explain it better:
protocol MyProtocol: class {
static func someIdentifier() -> String
}
class Object: NSObject, MyProtocol {
var name: String = "aloha"
static func someIdentifier() -> String {
return "This is it!"
}
}
class Other: NSObject {
var obj: Object?
}
let key = \Other.obj
let keyType = type(of: key)
let other = Other()
let value: Any? = other[keyPath: key]
if let object = value as? NSObject? {
if let protocolObject = object as? MyProtocol {
print("Yey!") // Can't get here :< What should I do to be able to call methods of MyProtocol?
// protocolObject.myIdentifier() // :< How?
}
}