本文会不定期更新,转载请注明出处http://blog.csdn.net/uxyheaven/article/details/47780957
Objective-C中条件语句的优化
要求判断一个NSArray有值,严谨的写法是
NSString *object = @"";
if (object && [object isKindOfClass:[NSArray class]] && ((NSArray *)object).count > 0)
{
NSLogDD
}
先判断这个object非空,再判断是NSArray类型,再判断count>0,如果顺序写成
NSString *object = @"";
if (object && ((NSArray *)object).count > 0 && [object isKindOfClass:[NSArray class]])
{
NSLogDD
}
则会运行错误,因为if里的代码是串行.
因此,对于多条件的if语句,尤其是大量运行的,我们可以调整 (条件) 的顺序,来改善代码效率.如:
[scores enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (obj > 59 && obj != 100)
{
NSLogDD
}
}];
当我们取考试及格,但是又不是满分的人,通常情况下就可以把 > 59 放到前面(通常情况下满分的人少).
当一段逻辑有多个if出现的情况,如:
+(UIControlEvents)eventWithName:(NSString *)name
{
if([name isEqualToString:@"UIControlEventTouchDown"]) return UIControlEventTouchDown;
if([name isEqualToString:@"UIControlEventTouchDownRepeat"]) return UIControlEventTouchDownRepeat;
if([name isEqualToString:@"UIControlEventTouchDragInside"]) return UIControlEventTouchDragInside;
if([name isEqualToString:@"UIControlEventTouchDragOutside"]) return UIControlEventTouchDragOutside;
if([name isEqualToString:@"UIControlEventTouchDragEnter"]) return UIControlEventTouchDragEnter;
if([name isEqualToString:@"UIControlEventTouchDragExit"]) return UIControlEventTouchDragExit;
if([name isEqualToString:@"UIControlEventTouchUpInside"]) return UIControlEventTouchUpInside;
if([name isEqualToString:@"UIControlEventTouchUpOutside"]) return UIControlEventTouchUpOutside;
if([name isEqualToString:@"UIControlEventTouchCancel"]) return UIControlEventTouchCancel;
if([name isEqualToString:@"UIControlEventTouchDown"]) return UIControlEventTouchDown;
if([name isEqualToString:@"UIControlEventValueChanged"]) return UIControlEventValueChanged;
if([name isEqualToString:@"UIControlEventEditingDidBegin"]) return UIControlEventEditingDidBegin;
if([name isEqualToString:@"UIControlEventEditingChanged"]) return UIControlEventEditingChanged;
if([name isEqualToString:@"UIControlEventEditingDidEnd"]) return UIControlEventEditingDidEnd;
if([name isEqualToString:@"UIControlEventEditingDidEndOnExit"]) return UIControlEventEditingDidEndOnExit;
if([name isEqualToString:@"UIControlEventAllTouchEvents"]) return UIControlEventAllTouchEvents;
if([name isEqualToString:@"UIControlEventAllEditingEvents"]) return UIControlEventAllEditingEvents;
if([name isEqualToString:@"UIControlEventApplicationReserved"]) return UIControlEventApplicationReserved;
if([name isEqualToString:@"UIControlEventSystemReserved"]) return UIControlEventSystemReserved;
if([name isEqualToString:@"UIControlEventAllEvents"]) return UIControlEventAllEvents;
return UIControlEventAllEvents;
}
我们可以通过 把多个if 改成if () else if… 来优化逻辑,同样可以合理的安排条件的顺序.
还有一种更加优美的方法,用字典来代替if, 如
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];
}
这里虽然有个装箱拆箱的过程,但是逻辑比大量的 if 清晰多了.
我们还可以用三目来优化代码,当然了这个只是说可以,实际不建议用.
如果真的的要用,请按照这种写法:
BOOL b = YES;
// 普通写法
if (b)
{
printf("11");
}
else
{
printf("22");
}
// 三目写法
(b) ?
({
printf("11");
})
:
({
printf("22");
});
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-27 08:45:28