Question

I'm saving a value to NSUserDefault.

Original value is in double, I convert it into NSNumber and then save it as a string. I printed the values while saving and they are set correctly.

When retrieving, i get the string value and convert it into double. But both the string and double value don't give anything. string gives null and double gives 0.000

Saving

//convert filtersequcne to NSNumber...finally saved as string

            NSNumber *myDoubleNumber = [NSNumber numberWithDouble:filtersequence];
            NSLog(@"Value being stored in myDoubleNumber %@",myDoubleNumber);


            // save string with input name of filter for that key
            NSUserDefaults *combination = [NSUserDefaults standardUserDefaults];
            [combination setObject:[myDoubleNumber stringValue] forKey:input];

            NSLog(@"Value of [mydoublenumber] string value %@",[myDoubleNumber stringValue]);

Getting the values back

//get input from saved filter
    NSUserDefaults *fetchcombination = [NSUserDefaults standardUserDefaults];

    // get the string
    NSString *message = [fetchcombination objectForKey:@"Test"];
    NSLog(@"Value of message %@",message);

    // convert string to double
    double actualdoublecombination = [message doubleValue];
    NSLog(@"Value of double actualdoublecombination %f",actualdoublecombination);
Was it helpful?

Solution

This is how you save/read a double:

double value = 42.00;
[[NSUserDefaults standardUserDefaults] setDouble:value forKey:@"MyDouble"];

double value = [[NSUserDefaults standardUserDefaults] doubleForKey:@"MyDouble"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top