Optimization of conditional statements in Objective-c

Source: Internet
Author: User
Tags ocaml

This article will not be regularly updated, reproduced please indicate the source http://blog.csdn.net/uxyheaven/article/details/47780957

Optimization of conditional statements in Objective-c

Ask to judge a nsarray to have value, rigorous wording is

    NSString *object = @"";    if (object && [objectclass]] && ((NSArray *)object0)    {        NSLogDD    }

First judge this object non-empty, then judge is Nsarray type, and then Judge Count>0, if the order is written

    NSString *object = @"";    if (object && ((NSArray *)object0 && [objectclass]])    {        NSLogDD    }

The error is run because the code in the If is serial.
Therefore, for multi-conditional if statements, especially for large numbers of operations, we can adjust the order of (conditions) to improve the efficiency of the Code. such as:

    [scores enumerateObjectsUsingBlock:^(idBOOL *stop) {        if59100)        {            NSLogDD        }    }];

When we pass the exam, but not the full score of the person, usually you can put > 59 in front (usually the number of people in the case of fewer).

When a section of logic has multiple if occurrences, such as:

+ (uicontrolevents) Eventwithname: (NSString *)name{if([nameisequaltostring:@"Uicontroleventtouchdown"])returnUicontroleventtouchdown;if([nameisequaltostring:@"Uicontroleventtouchdownrepeat"])returnUicontroleventtouchdownrepeat;if([nameisequaltostring:@"Uicontroleventtouchdraginside"])returnUicontroleventtouchdraginside;if([nameisequaltostring:@"Uicontroleventtouchdragoutside"])returnUicontroleventtouchdragoutside;if([nameisequaltostring:@"Uicontroleventtouchdragenter"])returnUicontroleventtouchdragenter;if([nameisequaltostring:@"Uicontroleventtouchdragexit"])returnUicontroleventtouchdragexit;if([nameisequaltostring:@"UIControlEventTouchUpInside"])returnUIControlEventTouchUpInside;if([nameisequaltostring:@"Uicontroleventtouchupoutside"])returnUicontroleventtouchupoutside;if([nameisequaltostring:@"Uicontroleventtouchcancel"])returnUicontroleventtouchcancel;if([nameisequaltostring:@"Uicontroleventtouchdown"])returnUicontroleventtouchdown;if([nameisequaltostring:@"Uicontroleventvaluechanged"])returnuicontroleventvaluechanged;if([nameisequaltostring:@"Uicontroleventeditingdidbegin"])returnUicontroleventeditingdidbegin;if([nameisequaltostring:@"Uicontroleventeditingchanged"])returnuicontroleventeditingchanged;if([nameisequaltostring:@"Uicontroleventeditingdidend"])returnUicontroleventeditingdidend;if([nameisequaltostring:@"Uicontroleventeditingdidendonexit"])returnUicontroleventeditingdidendonexit;if([nameisequaltostring:@"Uicontroleventalltouchevents"])returnuicontroleventalltouchevents;if([nameisequaltostring:@"Uicontroleventalleditingevents"])returnuicontroleventalleditingevents;if([nameisequaltostring:@"Uicontroleventapplicationreserved"])returnuicontroleventapplicationreserved;if([nameisequaltostring:@"Uicontroleventsystemreserved"])returnuicontroleventsystemreserved;if([nameisequaltostring:@"Uicontroleventallevents"])returnuicontroleventallevents;returnUicontroleventallevents;}

We can do this by changing multiple if to if () else if ... To optimize the logic, you can also arrange the order of the conditions reasonably.
There is also a more graceful way to use a dictionary instead of if, as

Xy_diccontrolstringevent = [@{@"Uicontroleventtouchdown": @ (Uicontroleventtouchdown), @"Uicontroleventtouchdownrepeat": @ (uicontroleventtouchdownrepeat), @"Uicontroleventtouchdraginside": @ (uicontroleventtouchdraginside), @"Uicontroleventtouchdragoutside": @ (uicontroleventtouchdragoutside), @"Uicontroleventtouchdragenter": @ (Uicontroleventtouchdragenter), @"Uicontroleventtouchdragexit": @ (Uicontroleventtouchdragexit), @"UIControlEventTouchUpInside": @ (UIControlEventTouchUpInside), @"Uicontroleventtouchupoutside": @ (uicontroleventtouchupoutside), @"Uicontroleventtouchcancel": @ (Uicontroleventtouchcancel), @"Uicontroleventvaluechanged": @ (uicontroleventvaluechanged), @"Uicontroleventeditingdidbegin": @ (Uicontroleventeditingdidbegin), @"Uicontroleventeditingchanged": @ (uicontroleventeditingchanged), @"Uicontroleventeditingdidend": @ (Uicontroleventeditingdidend), @"Uicontroleventeditingdidendonexit": @ (Uicontroleventeditingdidendonexit), @"Uicontroleventalltouchevents": @ (uicontroleventalltouchevents), @"Uicontroleventalleditingevents": @ (uicontroleventalleditingevents), @"Uicontroleventapplicationreserved": @ (uicontroleventapplicationreserved), @"Uicontroleventsystemreserved": @ (uicontroleventsystemreserved), @"Uicontroleventallevents": @ (Uicontroleventallevents)} retain];
+(UIControlEvents)eventWithName:(NSString *)name{    return [[XY_DicControlStringEvent objectForKey:name] integerValue];}

There is a process of packing and unpacking, but logic is much clearer than a large number of If.

We can also use three mesh to optimize the code, of course, this is just say yes, actually not recommended.
If you really want to use it, please follow this notation:

    BOOL b = YES;    // 普通写法    if (b)    {        printf("11");    }    else    {        printf("22");    }    // 三目写法    (b) ?    ({        printf("11");    })    :    ({        printf("22");    });

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Optimization of conditional statements in Objective-c

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.