Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Xcode provide autocompletion for dot-notated properties on objects of type id<protocol>?

Given this protocol definition:

@protocol MyProtocol <NSObject>
@property (nonatomic, strong) NSString *someProperty;
@end

Why will Xcode gladly offer autocompletion for this statement:

id<MyProtocol> thing = [ThingManager currentThing];
[thing someProperty]; // Xcode offered autocompletion here

But it doesn't offer autocompletion when I try to access the same property using dot-notation:

id<MyProtocol> thing = [ThingManager currentThing];
thing.someProperty; // Xcode claimed there were 
                    // "No completions" available
                    // after the period
like image 995
steveluscher Avatar asked Jan 26 '13 05:01

steveluscher


1 Answers

Because id is a base type, Xcode and CLANG are uneasy about providing dot-syntax access against it because dot syntax is just syntactic sugar for a method call to an associated setter or getter in a normal object, but id has no defined method members. Looking at it from the C side of things, id is a typedef for a struct pointer that the compiler cannot see the members of, which means it cannot access them (never mind the fact that you would need to dereference id before dot-access would make any semantic sense).

Back to the Objective-C side of things, protocols don't actually add methods or properties to the classes that claim to implement them, rather they serve as a specifier to other classes that an object that conforms to a given protocol implements a series of methods. As for the method-syntax being completed, Xcode pools all of the given methods of all the files imported into a given .m file because, an object of type id can receive any message*

*of course, it can receive the message, but it'll still crash if it's unimplemented.

like image 175
CodaFi Avatar answered Sep 20 '22 14:09

CodaFi