Usage Scenario: A server comes up with a set of data, such as a notification message in the app, where the type value represents the datatype
For a chestnut ~ for type and test text type (displayText), DisplayText used in the interface display
@ "Test1": @ "Test Type 1",
@ "Test2": @ "Test Type 2",
@ "Test3": @ "Test Type 3",
@ "test4": @ "Test Type 4",
@ "TEST5": @ "Test Type 5",
@ "Test6": @ "Test Type 6",
@ "TEST7": @ "Test Type 7"
Would you have thought of using the if to determine a type to get the corresponding text type?
Or do you define some enumeration types to determine the type, and then return the corresponding text type by using a switch based on the enumeration?
typedef ns_enum (Nsinteger, StringType) { stringtype_test1, stringtype_test2, stringtype_test3, Stringtype_test4, stringtype_test5, stringtype_test6, stringtype_test7, Stringtype_unknown, //unknown};
#pragmaMark--(StringType) Checkmessagetype: (NSString *) typename{if([TypeName isequaltostring:@"test1"]){ returnStringtype_test1; }Else if([TypeName isequaltostring:@"test2"]){ returnStringtype_test2; } Else if([TypeName isequaltostring:@"test3"]){ returnStringtype_test3; } Else if([TypeName isequaltostring:@"test4"]){ returnStringtype_test4; } Else if([TypeName isequaltostring:@"Test5"]){ returnStringtype_test5; } Else if([TypeName isequaltostring:@"Test6"]){ returnStringtype_test6; } Else if([TypeName isequaltostring:@"Test7"]){ returnstringtype_test7; } returnStringtype_unknown;}
-(NSString *) Checktypename: (stringtype) type{Switch(type) { Casestringtype_test1:{return @"Test message 1"; Break; } Casestringtype_test2:{return @"Test Message 2"; Break; } Casestringtype_test3:{return @"Test Message 3"; Break; } Casestringtype_test4:{return @"Test Message 4"; Break; } Casestringtype_test5:{return @"Test Message 5"; Break; } Casestringtype_test6:{return @"Test Message 6"; Break; } Casestringtype_test7:{return @"Test Message 7"; Break; } default: Break; } return @"";}
The displaytext code for the corresponding type is found at this time
NSString *typeifswitch = [self checktypename: [self checkmessagetype:type]];
So, how can I eliminate if else or switch?
the only prerequisite is to find the corresponding displaytext according to the type
Hint type is always different, can be said to be the only one
Speaking of the only one, have you ever thought of a thing? Dictionary? Yes, is the dictionary, because the dictionary key is unique, using a dictionary, only need to find the corresponding key value is DisplayText ~
So, what if else switch doesn't need to have the code has the truth
Nsdictionary *typedic = @{ @ "test1": @ "test message 1" @ " test2": @ "test message 2" @ " test3": @ "test message 3" @ " test4": @ " Test message 4 " @ test5": @ "test message 5" @ "Test6": @ "test message 6" @ " test7": @ "test message 7", }nsstring *typedictionary = Typedic[stringtxt];
is not the code on the thin body a lot of, and good intuitive to say
----------------------------------------------------------------------------------------
Then, let's experiment with the time spent on both of these methods.
----------------------------------------------------------------------------------------
typedef void (^nslogtime) ();
Time spent viewing code in Nslogtime-(void) log: (nslogtime) log{ if (log) { Cfabsolutetime start = Cfabsolutetimegetcurrent (); Log (); Cfabsolutetime end__ = Cfabsolutetimegetcurrent (); NSLog (@ "Time consuming%f", end__-start);} }
The dictionary spends less time
and replace it with a type
I
The dictionary spends roughly the same amount of time, and if and if it takes more time to increase the branching conditions, the following is the time to find the type
Do you want to remove the switch case or the IF statement?