What make me doubt is that the class object can invoke instance method.
The method of "methodSignatureForSelector is instance method
but when I invoke it by instance object, it go wrong.
NSString *classStr = @"NSInvocationObject";
// 获取class
Class objClass = NSClassFromString(classStr);
// 获取函数
SEL selector = NSSelectorFromString(@"classTest");
// 获取对象
Class stclass = [objClass class];
// - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector OBJC_SWIFT_UNAVAILABLE(""); here is a instance method ,but be invoke by objClass
NSMethodSignature *singature = [objClass methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:singature];
invocation.target = objClass;
invocation.selector = selector;
[invocation invoke];ere
Selector
hallomethod in one class has the same selector ashallomethod in another class.Selector just identifies method by name and it's irrelevant if it's a class or an instance.
-(void)hallo;-> selector =hallo+(void)hallo;-> selector =halloNSMethodSignature
Method signature describes type information of the return value and parameters. It doesn't say anything about method name, if it's an instance method, a class method, ... Just types.
-(void)hallo;- signature/encoding =v@:v- void, return type@- object:- selector+(void)hallo;- signature/encoding =v@:Type Encodings for more info.
To make it more obvious, imagine you have following methods:
Both of them have
fooas a selector, but they do differ in encoding:-(void)foo- signature/encoding =v@:+(NSUInteger)foo- signature/encoding =Q@:Usage
Here's an example. If your class method & instance method equals (name & return type & parameter types), you can use the same selector and method signature on the class and on the instance.
invokeWithTarget:says - send a message (identifier by selector = method name & signature = return value & parameter types) -targetcan be anything that can receive this message.Simplified a bit, but should explain what's going on here.
Sample class:
Invocations:
Documentation
Archived, but still good to learn about:
Comments
No. Check the
methodSignatureForSelector:methodaSelectorargument documentation:What selector identifies? Method by name and it's
halloin our case - NOT+(void)halloand NOT-(void)hallo, justhallo- this is what I meant with irrelevant comment.iis the receiver,iis an instance, so the following code asks for the-(void)hallomethod signature, not+(void)hallo.cis the receiver,cis a class, so the following code asks for the+(void)hallomethod signature, not-(void)hallo.There's also
+ (NSMethodSignature *)instanceMethodSignatureForSelector:(SEL)aSelectorwhich you can invoke on a class to get an instance method selector:I either don't understand what do you mean with this or you're misusing the instance method term.
Objective-C Classes Are also Objects:
Objects Send and Receive Messages
[i hallo]actually means send ahallomessage toi&[c hallo]actually means send ahallomessage tocwhereiis an instance andcis aClass.It seems to me that you're just using wrong term. If I understood your comment correctly, yes, you can send a message to either an instance or a class.
Here're other resources related to this topic: