How to find out who sent a message

This is the first post for stackoverflow, although I always check the forum.

I am looking for a convenient way to find out who sent a message to ObjC without sending a pointer as an argument in the method.

Can anyone help?

Thanks in advance!

+4
source share
3 answers

This is not possible in the general case (messages can be installed from places where I am not, such as runtime or the main function), and it is impractical even where it can be technically possible, because it will require you to walk on the glass and analyze there are bytes.

In practice, you do not need to know the sender in most cases, except for action methods. This is usually a sign of poor construction. And in any case, when you need to get a reference to another object, it should be passed as an argument to the method.

EDIT: I just stumbled upon this and noticed a comment. In case someone is wondering, the reason why he is often a sign of poor design is that he creates a tight connection between components that are almost never needed (again, outside of the action methods). You can usually take a delegate or callback block to achieve the same goal.

+3
source

No, this is not possible without pointing to the sender of the message, for example:

- (void) someMethod:(id) sender { } [obj someMethod:self]; 
+3
source

I think you could use the hash value of the base NSObject (if the object is a subclass of NSObject ). As an NSUInteger it will be the same size as the pointer, so there is not much space saving.

Why don’t you want to use a pointer?

0
source

All Articles