【发布时间】:2015-07-27 22:23:18
【问题描述】:
我有以下代码:(很长)
//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;
}
问题是,当控制到达case结束时,整个block会被重新执行(因此,NSLog在stdout中出现了两次)。
知道为什么会这样吗? 谢谢!
编辑:未删节的代码
【问题讨论】:
-
请显示真实代码。这里实际发生了什么(即周围环境)。
-
如果您重新阅读我对上一个问题的评论,我特别指出,当您发布此问题时,请务必提供更多上下文。仅仅发布没有上下文的
case声明并不能帮助我们帮助您。case语句中的大部分代码与问题无关。问题是为什么case会被意外调用两次,所以案件的主体并不重要。 -
顺便说一句 - 你做了什么调试来解决这个问题?在您的日志语句上放置一个断点并查看堆栈跟踪它停止的两次。查看导致它被调用两次的代码路径。
-
什么
switch声明?没有。 -
对所有人-对不起,我把这个问题搞砸了。会更新
标签: objective-c loops switch-statement