Why can I declare @objc protocols as weak properties but not fast protocols?

I found out that if I created such a protocol:

protocol MyProtocol { }

I can not do it:

weak var myVar: MyProtocol?

And I found a way to fix this by adding @objcto the protocol declaration:

@objc protocol MyProtocol { }

But why can this fix the error?

I assume that adding @objcprevents the structures from matching for the protocol, so the value of the gurranteed variable is a reference type. I'm right?

In addition, adding @objcdoes not allow me to add fast types such as [String: Any]. I would also like to know if there is another way to fix the error.

+4
source share
1 answer

, , (struct enums).

" "

protocol MyProtocol : class { }

:

weak var myVar: MyProtocol?

@objc protocol MyProtocol { }

, NSObject @objc, .

+4

Source: https://habr.com/ru/post/1654531/


All Articles