切换案例执行两次

时间:2015-05-15 21:18:44

标签: objective-c loops switch-statement

我有以下代码:(它很长)

//declarations (in the header) :
NSDictionary* batteryRawDict;
@property (strong, atomic, readonly) NSDictionary* batteryReport; //this dictionary is, obviously, @synthesize'd

-(BOOL) refreshGroup:(PFSystemKitGroup)group {
kern_return_t result;
switch (group) {
    case PFSKGroupGraphics: {
        val4Key("graphicReport", [self listGraphics]);
        //break;
    }
    case PFSKGroupBattery: { //to get more informations or to subscribe for events about power sources, use the IOPowerSources API
        if (!firstRunDoneForBattery) {
            batEntry = IOServiceGetMatchingService(masterPort, IOServiceMatching("IOPMPowerSource"));
            if (batEntry == 0) {
                _error = PFSKReturnComponentUnavailable;
                return false;
            }
        }
        CFMutableDictionaryRef  batProps = NULL;
        result = IORegistryEntryCreateCFProperties(batEntry, &batProps, NULL, 0);
        if (result!=kIOReturnSuccess) {
            _error = PFSKReturnIOKitCFFailure;
            _extError = result;
            return false;
        } else {
            batteryRawDict = (__bridge_transfer NSDictionary*)batProps;
            NSMutableDictionary* temp = [NSMutableDictionary.alloc init];
            if (!firstRunDoneForBattery) { //static keys
                //[temp setObject:[batteryRawDict objectForKey:@"DesignCapacity"] forKey:@"DesignedCapacity"];
                [temp setObject:[batteryRawDict objectForKey:@"DesignCycleCount9C"] forKey:@"DesignedCycleCount"];
                [temp setObject:[batteryRawDict objectForKey:@"BatterySerialNumber"] forKey:@"Serial"];
                [temp setObject:[batteryRawDict objectForKey:@"DeviceName"] forKey:@"Model"];
                [temp setObject:[batteryRawDict objectForKey:@"Manufacturer"] forKey:@"Manufacturer"];
                unsigned int manufactureDateAsInt = [[batteryRawDict objectForKey:@"ManufactureDate"] intValue];
                NSDateComponents* manufactureDateComponents = [[NSDateComponents alloc]init];
                manufactureDateComponents.year = (manufactureDateAsInt >> 9) + 1980;
                manufactureDateComponents.month = (manufactureDateAsInt >> 5) & 0xF;
                manufactureDateComponents.day = manufactureDateAsInt & 0x1F;
                [temp setObject:[[NSCalendar currentCalendar] dateFromComponents:manufactureDateComponents] forKey:@"ManufactureDate"];
                firstRunDoneForBattery = 1;
            }
            [temp setObject:[batteryRawDict objectForKey:@"BatteryInstalled"] forKey:@"isPresent"];
            [temp setObject:[batteryRawDict objectForKey:@"FullyCharged"] forKey:@"isFull"];
            [temp setObject:[batteryRawDict objectForKey:@"IsCharging"] forKey:@"isCharging"];
            [temp setObject:[batteryRawDict objectForKey:@"ExternalConnected"] forKey:@"isACConnected"];
            [temp setObject:[batteryRawDict objectForKey:@"Amperage"] forKey:@"Amperage"];
            [temp setObject:[batteryRawDict objectForKey:@"CurrentCapacity"] forKey:@"CurrentCapacity"];
            [temp setObject:[batteryRawDict objectForKey:@"MaxCapacity"] forKey:@"MaxCapacity"];
            [temp setObject:[batteryRawDict objectForKey:@"Voltage"] forKey:@"Voltage"];
            [temp setObject:[batteryRawDict objectForKey:@"CycleCount"] forKey:@"CycleCount"];
            [temp setObject:@(([[batteryRawDict objectForKey:@"MaxCapacity"] intValue] / [[batteryRawDict objectForKey:@"DesignCapacity"] intValue])*100) forKey:@"Health"]; //percentage
            [temp setObject:@([[batteryRawDict objectForKey:@"Temperature"] doubleValue] / 100) forKey:@"Temperature"];
            /*to be checked*/[temp setObject:@([[batteryRawDict objectForKey:@"Amperage"] doubleValue] / 1000 * [[batteryRawDict objectForKey:@"Voltage"] doubleValue] / 1000) forKey:@"Power"];
            NSDateComponents* differenceDate = [[NSCalendar currentCalendar] components:NSCalendarUnitDay
                                            fromDate:[temp objectForKey:@"ManufactureDate"]
                                              toDate:[NSDate date]
                                             options:0];
            [temp setObject:@([differenceDate day]) forKey:@"Age"];
            NSLog(@"--------------------------------------------------------");
            batteryReport = [temp copy];
        }
        _error = PFSKReturnSuccess;
        return true;
    }
}
_error = PFSKReturnSuccess;
return true;
}

问题是,当控件到达案例结尾时,整个块将被重新执行(因此,在stdout中出现两次NSLog时)。

有关为何发生这种情况的任何想法? 谢谢!

编辑:未删节的代码

1 个答案:

答案 0 :(得分:1)

感谢@rmaddy,我查看了堆栈跟踪。

在那里,我注意到案例在预期时执行了一次,但是在之前,同时也是前一个案例......这显示缺少其break语句,所以控制继续执行。最后,请记住检查所有的案例是否有中断语句。

再次感谢@rmaddy