有人可以向我解释为什么我的变量被称为活动时变量为0时定义为浮点数,但在定义为int时显示正确的值?我有另一个漂浮物,用于几乎相同的东西,但工作完美!
@implementation AppPrefsViewController
float age;
int activity;
...
-(void) updateAValues{
if (selectedActivityUnit == 0){
activity = 50;
NSLog(@"activity = %d",activity);
}
if (selectedActivityUnit == 1){
activity = 75;
NSLog(@"activity = %d",activity);
}
}
....
- (void)updateLabel {
if (selectedUnit == 0) {
subView.hidden = YES;
age = 1;
NSLog(@"age = %d",age);
}
if (selectedUnit == 1) {
subView.hidden = YES;
age = 2;
NSLog(@"age = %d",age);
}
if (selectedUnit == 2) {
subView.hidden = NO;
age = 3;
NSLog(@"age = %d",age);
}
}
答案 0 :(得分:3)
你正在使用
NSLog(@"activity = %d", activity);
显示您的值。如果activity是int类型,则此方法有效,因为%d用于显示整数。如果要显示浮动,则需要浮点格式化程序%f,如:
NSLog(@"activity = %f", activity);