我将值保存到NSUserDefault。
原始值为double,我将其转换为NSNumber,然后将其保存为字符串。 我在保存时打印了值,并且设置正确。
检索时,我得到字符串值并将其转换为double。但是字符串和双重值都没有给出任何东西。 string给出null,double给出0.000
保存
//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]);
重新获得价值
//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);
答案 0 :(得分:2)
这是您保存/阅读双倍的方式:
double value = 42.00;
[[NSUserDefaults standardUserDefaults] setDouble:value forKey:@"MyDouble"];
double value = [[NSUserDefaults standardUserDefaults] doubleForKey:@"MyDouble"];