简体   繁体   中英

What a warning with protocol forward declaration?

I just wanna avoid warning (without compiler suppression), but don't wanna add some includes to my file. Here is my code:

@protocol MyProto;

// ...

Protocol *p = @protocol(MyProto);

Here is warning I got:

@protocol is using a forward protocol declaration of MyProto [-Wat-protocol]

Using a protocol literal ( @protocol ) requires linking to the protocol at compile time, which requires the compiler to see the definition of the protocol.

I don't understand why you are averse to "add some includes to my file". It's very normal to include header files defining something in an implementation file that is using that thing. There should be no downside.

If you want to look up the protocol at runtime from a string you can do something like NSProtocolFromString(@"MyProto") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM