canyingwushang
canyingwushang

Reputation: 13

calls to instanceMethodSignatureForSelector return nil.Why?

The Header File of NSString has the function declartion like this:

- (instancetype)initWithBytes:(const void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding;    

Here is the demo code.

SEL selector1 = @selector(initWithBytes:length:encoding:);    
NSLog(@"%@", [NSString instanceMethodSignatureForSelector:selector1]);    
NSLog(@"%@", [[NSString new] methodSignatureForSelector:selector1]);    

But both of the results are: (null). Why? Can any function run without MethodSignature?

The enviroment: XCode6.2 Beta. iPhone Simulator.

Any help is appreciated! Thank you very much!

Upvotes: 1

Views: 940

Answers (1)

matt
matt

Reputation: 535526

Yes, a method can run without a method signature, if some other object has the method signature and responds to the method. Basically, you've stumbled onto an implementation detail. NSString does not in fact respond to initWithBytes:length:encoding:, as you can discover by asking it:

SEL selector1 = @selector(initWithBytes:length:encoding:);
NSLog(@"%d", [[NSString class] instancesRespondToSelector:selector1]); // 0

It is presumably being forwarded dynamically to a helper object of some other class that you're not supposed to know about. You can use this same feature in your own code.

Upvotes: 1

Related Questions