xcode定义变量问题

发布于 2024-09-24 09:32:35 字数 704 浏览 4 评论 0原文

有人可以向我解释一下为什么我的名为 Activity 的变量在定义为 float 时为 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);
 }
}

Can someone please explain to me why my variable called activity is 0 when defined as a float, but displays the correct value when defined as an int? I have another float which is used for pretty much the same thing but works perfectly!

@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);
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦妄 2024-10-01 09:32:35

您正在使用

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

来显示您的价值观。如果 Activity 是 int 类型,则此方法有效,因为 %d 用于显示 int。如果要显示浮点数,则需要浮点数格式化程序 %f,如下所示:

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

You're using

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

To display your values. This works if activity is an int type as %d is for displaying ints. If you want to display floats you need the float formater %f, as in:

NSLog(@"activity = %f", activity);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文