xcode定义变量问题
有人可以向我解释一下为什么我的名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
来显示您的价值观。如果 Activity 是 int 类型,则此方法有效,因为 %d 用于显示 int。如果要显示浮点数,则需要浮点数格式化程序 %f,如下所示:
You're using
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: