문제

I am almost sure that this is not possible, but I'm wondering if someone more knowledgeable about Objective-C has a different idea. The reason I doubt this will work is that, while it may be possible with scary runtime-hackery, I doubt the compiler would be capable of understanding.

What I am wondering is whether it is possible to have a protocol that is interchangeable with another:

 // let <ShortName> be a synonym for <NSReallyReallyLongName>
 id <ShortName> obj = [NSSomething objectConformingtoReallyReallyLongProtocolName];

Essentially, I'd like to generalize my API to take objects that that conform to <NSReallyReallyLongName>, but I'd like both me and consumers of my API to not have to type so much. One possible solution is a typedef:

typedef id <NSReallyReallyLongName> ShortName;

But this makes it less clear for API consumers that I'm establishing a protocol-based constraint. Any ideas?

Update:

I feel sort of stupid, upon seeing the really simple solution in the answers below. Thanks for humoring me.

도움이 되었습니까?

해결책

Haven't tried this, but how about having short name protocol containing the message definitions, and long name protocol extend it with no new message definitions. Because short name is the parent, anything that returns an instance of long name should cast no problems and your users can use short name.

But from a KISS viewpoint, etc. I'd really suggest that you rethink and avoid this whole idea altogether. It's more likely to confuse than help.

다른 팁

#define ShortName LongName

You could also try @compatibility_alias, though that might just be for classes.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top