简体   繁体   English

在目标C中赋值后,浮点值始终返回1

[英]float value always return as 1 after assigning in objective-c

I am storing float value in UserDefaults. 我在UserDefaults中存储浮点值。

[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithFloat:self.labelSlider.lowerValue] forKey:@"KEYSLIDERLOWERVALUE"];
[[NSUserDefaults standardUserDefaults] synchronize];

but when I fetch value in variable "lowerVal", proper value assign to float variable but when I am going to assign float "lowerVal" to self.lableSlider.lowerValue it always give me 1. 但是,当我在变量“ lowerVal”中获取值时,将正确的值分配给float变量,但是当我要将浮点“ lowerVal”分配给self.lableSlider.lowerValue时,它总是给我1。

float lowerVal = [[[NSUserDefaults standardUserDefaults] valueForKey:@"KEYSLIDERLOWERVALUE"] floatValue];

if (lowerVal > [[self.arrPriceRange valueForKey:@"minPrice"] floatValue]) {
        self.labelSlider.lowerValue = lowerVal;
    }else{
        self.labelSlider.lowerValue = [[self.arrPriceRange valueForKey:@"minPrice"] floatValue];
    }

in short when I print lowerVal in console it gives me 4.55(correct value) but when I print self.lableSlider.lowerValue it gives me 1. 简而言之,当我在控制台中打印lowerVal时,它会给我4.55(正确值),但是当我打印self.lableSlider.lowerValue时,它会给我1。

How can I solve this problem? 我怎么解决这个问题?

You can use this code: 您可以使用以下代码:

Setting Default Values 设置默认值

-(void) saveFloatValue:(float)x forKey:(NSString *)key {
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setFloat:x forKey:key];
    [userDefaults synchronize];
}

Getting Default Values 获取默认值

-(float) retrieveFloatValueWithKey:(NSString *)key {
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    return [userDefaults floatForKey:key];
}

Source: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/#//apple_ref/occ/instm/NSUserDefaults/setFloat:forKey: 来源: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/#//apple_ref/occ/instm/NSUserDefaults/setFloat:forKey:

Are you using NMRangeSlider? 您正在使用NMRangeSlider吗? I've checked the source code and found: 我检查了源代码,发现:

- (void) setLowerValue:(float)lowerValue
{
    float value = lowerValue;

    if(_stepValueInternal>0)
    {
        value = roundf(value / _stepValueInternal) * _stepValueInternal;
    }

    value = MIN(value, _maximumValue);
    value = MAX(value, _minimumValue);

    if (!isnan(_lowerMaximumValue)) {
        value = MIN(value, _lowerMaximumValue);
    }

    value = MIN(value, _upperValue - _minimumRange);

    _lowerValue = value;

    [self setNeedsLayout];
}

Take care of these lines: 请注意以下几行:

value = MIN(value, _maximumValue);
value = MAX(value, _minimumValue);

The default value of _maximumValue is 1.0, so lowerValue can not exceed it. _maximumValue的默认值为1.0,因此lowerValue不能超过它。 In this case, you should set maximumValue and minimumValue due to your requirement. 在这种情况下,应根据需要设置maximumValue和minimumValue。

You are using wrong methods to access values in NSUserDefaults . 您使用错误的方法访问NSUserDefaults值。 Exchange 交换

  • valueForKey: with objectForKey: valueForKey:objectForKey:
  • setValue:forKey: with setObject:forKey: setValue:forKey:setObject:forKey:

In your case you could also use the specialized methods for floating point types, which do the boxing/unboxing for you: 在您的情况下,您还可以对浮点类型使用专门的方法,这些方法可以为您装箱/拆箱:

[[NSUserDefaults standardUserDefaults] setFloat:self.labelSlider.lowerValue forKey:@"KEYSLIDERLOWERVALUE"];

float lowerVal = [[NSUserDefaults standardUserDefaults] floatForKey:@"KEYSLIDERLOWERVALUE"];

As a sidenote: calling synchronize is not necessary and just a waste of resources. 附带说明:调用synchronize不是必需的,只是浪费资源。

The methods you've been using ( valueForKey: and setValue:forKey: ) are KVC accessors that are not expected or documented to work in NSUserDefaults . 您一直在使用的方法( valueForKey:setValue:forKey:是KVC访问器,这些访问器在NSUserDefaults中是不期望或没有记录的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM