【问题标题】:xcode defining variable problemxcode定义变量问题
【发布时间】:2010-09-20 23:49:56
【问题描述】:

有人可以向我解释为什么我的变量 activity 在定义为浮点数时为 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);
 }
}

【问题讨论】:

    标签: iphone xcode variables floating-point int


    【解决方案1】:

    你正在使用

    NSLog(@"activity = %d", activity);
    

    显示你的价值观。如果活动是 int 类型,则此方法有效,因为 %d 用于显示 int。如果要显示浮点数,则需要浮点格式器 %f,如:

    NSLog(@"activity = %f", activity);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-22
      • 2019-09-21
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多