原文:http://www.cocoachina.com/industry/20140816/9397.html
- switch(condition) {
- case value1:
- result = @"valueFor1";
- break;
- case value2:
- result = @"valueFor2";
- break;
- case value3:
- result = @"valueFor3";
- break;
- case value4:
- result = @"valueFor4";
- break;
- case value5:
- result = @"valueFor5";
- break;
- default:
- result = @"valueForDefault";
- break;
- }
- return result;
- static NSDictionary *mapping = nil;
- if(!mapping) {
- mapping = @{
- @(value1) : @"valueFor1",
- @(value2) : @"valueFor2",
- @(value3) : @"valueFor3",
- @(value4) : @"valueFor4"
- @(value5) : @"valueFor5"
- };
- }
- return mapping[@value] ?: @"valueForDefault";
- if ([title isEqualToString:@"Scratches"])
- {
- title = [NSString stringWithFormat:(self.vehicle.numberOfScratches == 1 ? @"%d Scratch" : @"%d Scratches"), self.vehicle.numberOfScratches];
- }
- else if ([title isEqualToString:@"Dents"])
- {
- title = [NSString stringWithFormat:(self.vehicle.numberOfDents == 1 ? @"%d Dent" : @"%d Dents"), self.vehicle.numberOfDents];
- }
- else if ([title isEqualToString:@"Painted Panels"])
- {
- title = [NSString stringWithFormat:(self.vehicle.numberOfPaintedPanels == 1 ? @"%d Painted Panel" : @"%d Painted Panels"), self.vehicle.numberOfPaintedPanels];
- }
- else if ([title isEqualToString:@"Chips"])
- {
- title = [NSString stringWithFormat:(self.vehicle.numberOfChips == 1 ? @"%d Chip" : @"%d Chips"), self.vehicle.numberOfChips];
- }
- else if ([title isEqualToString:@"Tires"])
- {
- title = [NSString stringWithFormat:(self.vehicle.numberOfTires == 1 ? @"%d Tire" : @"%d Tires"), self.vehicle.numberOfTires];
- }
- else title = nil;
- static NSDictionary *titleMapping = nil;
- if (!titleMapping) {
- NSString *(^const format)(NSUInteger, NSString *, NSString *) = ^(NSUInteger value, NSString *singular, NSString *plural) {
- return [NSString stringWithFormat:@"%d %@", value, (value == 1 ? singular : plural)];
- };
- titleMapping = @{
- @"Scratches" : ^(MyClass *target) {
- return format([target numberOfScratches], @"Scratch", @"Scratches");
- },
- @"Dents" : ^(MyClass *target) {
- return format([target numberOfDents], @"Dent", @"Dents");
- },
- @"Painted Panels" : ^(MyClass *target) {
- return format([target numberOfPaintedPanels], @"Painted Panel", @"Painted Panels");
- },
- @"Chips" : ^(MyClass *target) {
- return format([target numberOfChips], @"Chip", @"Chips");
- },
- @"Tires" : ^(MyClass *target) {
- return format([target numberOfTires], @"Tire", @"Tires");
- }
- };
- }
- NSString *(^getTitle)(MyClass *target) = titleMapping[title];
- return getTitle ? getTitle(self) : nil;
- if(!error) {
- //! success code
- } else {
- //! failure code
- }
- if(error) {
- //! failure code
- return;
- }
- //! success code
- if([type isEqualToString:@"videoWidget"]) {
- [self parseVideoWidget:dictionary];
- } else
- if([type isEqualToString:@"imageWidget"]) {
- [self parseImageWidget:dictionary];
- } else
- if([type isEqualToString:@"textWidget"]) {
- [self parseTextWidget:dictionary];
- } else
- if([type isEqualToString:@"twitterWidget"]) {
- [self parseTwitterWidget:dictionary];
- }
- SEL dynamicSelector = NSSelectorFromString([NSString stringWithFormat:@"parse%@:", type]);
- if(![self respondsToSelector:dynamicSelector]) {
- DDLogWarning(@"Unsupported widget type %@", type);
- return;
- }
- [self performSelector:dynamicSelector withObject:dictionary];