IOS implements one method with multiple return values in three ways.

Source: Internet
Author: User

IOS implements one method with multiple return values in three ways.

I used to think that this title is a bit biased towards theory. How can this strange requirement be found in actual development? However, when such a hard requirement is met, I still think it is of some value, the theory has been put into practice.

Taking a piece of code I developed in private as an example, the intention is to have such a method: the ability to pass in a starting tag (NSString *), an ending tag (NSString *), a text (NSString *) then scan the text internally and return the range of the content marked as the package (the range of the nsange is ignored). This range may have multiple, so the returned value should be an array containing the range. By the way, the start and end tags of the original string are completely filtered out, And the filtered string is also returned.

For example, you need to input the Start mark "<" End mark ">" text "The meeting needs to be called <Peter> and <Robin>" and then you want to return an array [{location: 6, length: 2}, {location: 9, length: 2}], and return the processed string "The meeting needs to be called Peter and Robin ".

The code can be written in this way, but it is impossible.

- (NSArray *,NSMutableString *)scanBeginStr:(NSString *)beginstr endStr:(NSString *)endstr inText:(NSMutableString *)text

 

Below are three ways to fulfill this requirement.

1. Use a dictionary

This method is the least but easiest to understand. If you need to return multiple objects, you can directly put multiple objects in a dictionary and set a proper key and return the dictionary, the dictionary can contain any number of "return values ".

- (NSDictionary *)scanBeginStr:(NSString *)beginstr endStr:(NSString *)endstr inText:(NSMutableString *)text{    NSRange range1,range2;    NSUInteger location =0,length=0;    range1.location = 0;    NSMutableArray *rangeArray = [NSMutableArray array];    while (range1.location != NSNotFound) {        range1 = [text rangeOfString:beginstr];        range2 = [text rangeOfString:endstr];        if (range1.location != NSNotFound) {            location = range1.location;            length = range2.location - range1.location - 1;            if (length > 5000)break;            [text replaceOccurrencesOfString:beginstr withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, range1.location + range1.length)];            [text replaceOccurrencesOfString:endstr withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, range2.location + range2.length - 1)];        }        [rangeArray addObject:@{@"location":@(location),@"length":@(length)}];    }    return @{@"rangeArray":rangeArray,@"text":text};}

This is also the case when calling this method, which is very simple code.

    NSDictionary* result = [self scanBegin2Str:@"<" endStr:@">" inText:mutableText];    NSArray *rangeArray = result[@"rangeArray"];    NSMutableString *text = [result[@"text"] mutableCopy];

If the dictionary is uncomfortable, you can use a model to customize an object, assign values to each attribute of the object, and then return the custom object, although the Code looks a little more scientific, you need to write some additional code and cannot implement any configuration (each attribute must be set in advance ), this is the same idea as above and won't be described separately.

 

2. Use Pointer

This method is actually used, that is, to pass the pointer of the text pointer to be modified into the method, and then retrieve the pointer worth the text pointer from the method. Then, use this pointer to modify the value of an external variable. The code implementation is as follows:

- (NSArray *)scanBeginStr:(NSString *)beginstr endStr:(NSString *)endstr inText:(NSMutableString * *)textPointer{    NSRange range1,range2;    NSUInteger location =0,length=0;    range1.location = 0;    NSMutableString *text = *textPointer;    NSMutableArray *rangeArray = [NSMutableArray array];    while (range1.location != NSNotFound) {        range1 = [text rangeOfString:beginstr];        range2 = [text rangeOfString:endstr];        if (range1.location != NSNotFound) {            location = range1.location;            length = range2.location - range1.location - 1;            if (length > 5000)break;            [text replaceOccurrencesOfString:beginstr withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, range1.location + range1.length)];            [text replaceOccurrencesOfString:endstr withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, range2.location + range2.length - 1)];        }        [rangeArray addObject:@{@"location":@(location),@"length":@(length)}];    }    return rangeArray;}

This method is written in the call, because the mutabletext modification is silent.

NSArray * rangeArray = [self scanBegin3Str: @ "<" endStr: @ ">" inText: & mutableText]; // Dong borran blog

  

3. block callback

In fact, this method cannot calculate the return value, but it can achieve the effect of the return value.

- (void)scanBeginStr:(NSString *)beginstr endStr:(NSString *)endstr inText:(NSMutableString *)text result:(void(^)(NSArray *rangeArray,NSMutableString *text))result{    NSRange range1,range2;    NSUInteger location =0,length=0;    range1.location = 0;    NSMutableArray *rangeArray = [NSMutableArray array];    while (range1.location != NSNotFound) {        range1 = [text rangeOfString:beginstr];        range2 = [text rangeOfString:endstr];        if (range1.location != NSNotFound) {            location = range1.location;            length = range2.location - range1.location - 1;            if (length > 5000)break;            [text replaceOccurrencesOfString:beginstr withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, range1.location + range1.length)];            [text replaceOccurrencesOfString:endstr withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, range2.location + range2.length - 1)];        }        [rangeArray addObject:@{@"location":@(location),@"length":@(length)}];    }    result(rangeArray,text);}

This block may be special when used.

    [self scanBeginStr:@"<" endStr:@">" inText:mutabletext result:^(NSArray *rangeArray, NSMutableString *text) {        NSLog(@"%@,%@",rangeArray,text);    }];

If you can write the block return value as a dictionary or model, you can do that. The return value cannot be of the struct type, and the OC object in the structure cannot be of the basic data type.

In fact, there are other methods. For example, you can set N member variables after calculation in the method and re-set them. But you may also know that the member variables are more disgusting. Recently, the fire of functional programming has been advocating "no side effects in the method" and "Implementation of reference transparency". If so, the latter two methods do not comply with the FP rules, however, they also have their own characteristics.

 

The source must be indicated for reprinting. The original Article is a link.

  

Related Article

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.