I write a protocol (in Xcode playground),and want it be conform by specified class , eg: Root.
Because I used Swift 4.1 , So there are new syntax for this :
protocol Delegate where Self:Root{
var titleImage:UIImage? {get}
}
(PS:If you want to know more about this , please follow the link : How to make Swift protocol conformed by a specific kind of Class?)
And I also write class Root and Foo :
class Root:Delegate{
var titleImage:UIImage?
func willDo(){
let foo = Foo()
foo.delegate = self
foo.invoke()
}
}
class Foo{
var delegate:Delegate!
func invoke(){
if let image = delegate.titleImage{
print("have a image [\(image)")
}else{
print("not have a image")
}
}
}
Problem is that when I call :
let r = Root()
r.willDo()
Here are runtime error :
But when I changed Delegate code to this :
protocol Delegate{
var titleImage:UIImage? {get}
}
That's all right!!! But it can't limited Delegate to be conform by class Root anymore!
Could anyone can tell me Why is it? Why when add " Self:Root " to Delegate will trigger a BAD_ACCESS Runtime error???
Is this a Bug in Swift or I misunderstand something???
Thanks a lot ;)
1 Answer 1
Add @objc when declaring your protocol
@objc protocol Delegate where Self: Root{
var titleImage: UIImage? {get}
}
This is a current bug in swift so wrapping it in @objc should work
whereclauses isn't something that's fully supported currently – that's tracked by SR-6001. The specific runtime crash you're getting looks like SR-6816.