Common iOS design patterns-command design patterns and ios command Design Patterns
Detailed description of command Design Mode
- Detailed description of command Design Mode
- Basic Concepts
- Use of NSInvocation
- Command mode
Basic Concepts
The command design mode encapsulates a request or line action as an object. This encapsulated request is more flexible than the original request and can be passed, stored, dynamically modified, or put into the queue before the object. Apple implements this mode using the Target-Action mechanism and Invocation.
Use of NSInvocation
There are two message methods for calling an object directly in iOS:
One is javasmselector: withObject: The other is NSInvocation. The first method is relatively simple and can complete simple calls. However, if you want to process more than two parameters or have returned values, you need to do some additional work. In this case, we can use NSInvocation to perform these relatively complex operations.
NSInvocation can process parameters and return values. All java users know the reflection operation. In fact, NSInvocation is equivalent to the reflection operation.
Int main (int argc, const char * argv []) {ngutoreleasepool * pool = [[ngutoreleasepool alloc] init]; MyClass * myClass = [MyClass alloc] init]; NSString * myString = @ "My string"; // call NSString * normalInvokeString = [myClass appendMyString: myString]; NSLog (@ "The normal invoke string is: % @", normalInvokeString); // NSInvocation call SEL mySelector = @ selector (appendMyString :); NSMethodSignature * sig = [[myClass class] handler: mySelector]; NSInvocation * myInvocation = [NSInvocation usage: sig]; [myInvocation setTarget: myClass]; [myInvocation setSelector: mySelector]; [myInvocation setArgument: & myString atIndex: 2]; NSString * result = nil; [myInvocation retainArguments]; [myInvocation invoke]; [myInvocation getReturnValue: & result]; NSLog (@ "The NSInvocation invoke string is: % @", result); [myClass release]; [pool drain]; return 0;} MyClass. h # import <Foundation/Foundation. h> @ interface MyClass: NSObject {}-(NSString *) appendMyString :( NSString *) string; @ endMyClass. m # import "MyClass. h "@ implementation MyClass-(id) init {self = [super init]; if (self) {// Initialization code here .} return self;}-(NSString *) appendMyString :( NSString *) string {NSString * mString = [NSString stringWithFormat: @ "% @ after append method", string]; return mString ;} -(void) dealloc {[super dealloc];} @ end
[MyInvocation setArgument: & myString atIndex: 2]; why is index starting from 2? The reason is: 0 1 has been occupied by target and selector.
Command mode
NSMethodSignature * sig = [self methodSignatureForSelector: @ selector (addAlbum: atIndex :)]; NSInvocation * undoAction = [Unknown: sig]; [undoAction setTarget: self]; [undoAction setSelector: @ selector (addAlbum: atIndex :)]; [undoAction setArgument: & deletedAlbum atIndex: 2]; [undoAction setArgument: & currentAlbumIndex atIndex: 3]; [undoAction failed]; [undoStack addObject: undoAction];-(void) undoAction {if (undoStack. count> 0) {NSInvocation * undoAction = [undoStack lastObject]; [undoStack removeLastObject]; [undoAction invoke] ;}} the NSInvocation object at the top of the stack is displayed and called through invoke