3

It seems like instanceMethodSignatureForSelector is a class method, while methodSignatureForSelector is an instance method. However, their description and use cases do not bring me to this conclusion. So what is the difference?

2 Answers2

2

As we have enough info about the Messaging and Message forwarding concepts, lets discuss the solution. In the project I am currently working on , there is a search feature. User can search for results using some criteria. This criteria consists of primary and advanced criteria, and datamodel classes are in place for both. Primary datamodel will contain secondary datamodel as a property. There is a framework which takes care of mapping database tables to specified datamodels. To save or retrieve data, the framework will be sending message to the mapped datamodel class. This framework doesn’t support nested mappings. Primary datamodel class is mapped to the database table, hence the framework will be expecting the columns to be mapped to the properties and will be sending setter and getter messages to the primary datamodel class. Solution is to make the advanced data model a surrogate of the primary data model. In the primary datamodel class, by overriding the following methods, the secondary datamodel is set up as the surrogate for the primary datamodel class.

- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector 
{ 
    NSMethodSignature* signature = [super methodSignatureForSelector:selector];
    if (!signature) { 
        signature = [surrogate instanceMethodSignatureForSelector:selector]; 
    } 
    return signature; 
}

- (void)forwardInvocation:(NSInvocation *)anInvocation 
{ 
    if ([surrogate respondsToSelector:[anInvocation selector]]) 
        [anInvocation invokeWithTarget:surrogate]; 
    else 
        [super forwardInvocation:anInvocation]; 
} 

If I had used the initial approach of manually adding setters and getters, I would have ended up writing as many methods as twice the number of properties and had to make sure all the method signatures mimics the advanced criteria properties. Any changes to the name of the properties is a good entry point for regressin and code maintenance is going to be a mess. Other approach is to maintain a single datamodel which will represent all the criteria which will create other issues as the requirements for iPhone and iPad are different. Message forwarding approach reduced the amount of manual effort required and will be scalable even if there are changes to the datamodels.

Refrence : http://blog.imaginea.com/message-forwarding-and-surrogate-objects-in-objective-c/

Community
  • 1
  • 1
Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
1

While instanceMethodSignatureForSelector is a function you'd invoke to get signatures of any class's methods, methodSignatureForSelector is something you would primarily overrride to provide alternative methods to the runtime if it looks up a method of a protocol.

(Jay's answer is giving an example of that but I thought I should offer this short explanation in case you haven't come to this conclusion yourself yet.)

Thomas Tempelmann
  • 11,045
  • 8
  • 74
  • 149