Common iOS design patterns-command design patterns and ios command Design Patterns

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.