3

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 :

enter image description here

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 ;)

asked May 1, 2018 at 1:28
4
  • AFAIK being able to use class constraints in protocol where clauses 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. Commented May 1, 2018 at 1:34
  • @NaderBesada Is your Swift 4.1 in Version 9.3 (9E145) ??? Ary you mean my Xcode Version is too old??? Commented May 1, 2018 at 1:38
  • So it's seem be a Bug??? @Hamish Commented May 1, 2018 at 1:39
  • Restart Xcode or test in real project (not in playground) are all not working... But thank you anyway ;) @NaderBesada Commented May 1, 2018 at 1:45

1 Answer 1

2

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

answered May 1, 2018 at 1:46
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.