我通过简单地尝试在 obj-c 中传递一个浮点参数来处理一些非常奇怪的问题。
我有一个通过 NSNotification 调用的方法:
- (void)presetChanged:(NSNotification*)notification {
float newValue = [[S76PresetManager sharedManager] getValueForKey:symbol];
[self setValue:newValue animated:YES];
[PdBase sendFloat:newValue toReceiver:symbol];
}
当我通过调试器运行此命令时, float (例如 0.5)将保持不变并被发送到 [PdBase sendFloat:] 行。然而,在 [self setValue:animated:] 调用中,我得到一个垃圾值 3.68934881e+19。这很奇怪。有什么建议吗?
提前致谢
编辑:[self setValue:animated:] 中没有发生什么特别的事情,只是在将其发送到另一个方法之前进行一些边界检查...
- (void)setValue:(float)newValue animated:(BOOL)animated {
float oldValue = value;
if (newValue < 0.0)
value = 0.0;
else if (newValue > maxValue)
value = maxValue;
else
value = newValue;
[self valueDidChangeFrom:oldValue to:value animated:animated];
}
最佳答案
假设你在 [self setValue:newValueanimated:YES] 中没有做任何奇怪的事情...
怎么样:
NSNumber *newValue = [NSNumber numberWithFloat:[[S76PresetManager sharedManager] getValueForKey:symbol]];
[self setValue:newValue animated:YES];
[PdBase sendFloat:[newValue floatValue] toReceiver:symbol];
关于iphone - 传递 float 给出垃圾值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7249527/