i have an issue in swift where i want to check for protocol conformance in a class func and then use some static property when the check succeeds. Here's the playground code:
import UIKit
@objc protocol SearchableObject {
static var sortDescriptors: [NSSortDescriptor] { get }
}
class Test: NSObject {
}
extension Test {
class func loadData() {
if self is SearchableObject {
println(valueForKey("sortDescriptors"))
}
else {
println("not searchable")
}
}
}
class SearchableTest: Test, SearchableObject {
class var sortDescriptors: [NSSortDescriptor] {
return [NSSortDescriptor(key: "property", ascending: true)]
}
}
Test.loadData() // prints "not searchable"
SearchableTest.loadData() // prints "Optional((\n "(property, ascending, compare:)"\n))"
the problem is i can check for protocol conformance, but i can't cast self, that's why i'm using valueForKey().
(self as SearchableObject).sortDescriptors
^ 'SearchableObject' does not have a member named 'sortDescriptors'
casts self to an instance conforming to SearchableObject making the static var inaccessible. As don't like the valueForKey() approach i want to ask whether there is a better way to achieve this?
Thanks in advance :)