如何将整数值保存到NSuserDefault,如sqlite? [英] How to save integer value to NSuserDefault like sqlite?
本文介绍了如何将整数值保存到NSuserDefault,如sqlite?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在NSUserDefault中保存和检索值。我正在保存并成功检索Value。但是,当我去cak主页并再次进入我的班级时,不存储该值,并且所有过程再次完成。它不记得旧价值。
I want to save and retrieve value in NSUserDefault. I am saving and retrieving Value sucessfully. But when I go cak Homepage and again come in my class The value is not stored and all the process done again. It doesnot Remember old value.
我这样做 -
-(void)ViewDidLoad{
A = 0; // int A; defone in .h
[self calling_Once];
}
-(void)calling_Once{
NSLog(@"A is: %d",A); // A is always zero when I leave this class and return Back to this class. This is my only problem. I want A=1; when I go back And return to this class but
NSInteger B = [[NSUserDefaults standardUserDefaults] integerForKey:@"save_Interger_Value_For_Formula_One"]; //is not able to remeber value. It gives A=0; B =0; When I come back to this class, I want It should return A=1; B=1; and remember old value like sqlite when I return to this class.
if (A==0) {
[self refreshButton];
NSLog(@"Before Increment A is: %d",A);
A++;
NSLog(@"After Increment A is: %d",A);
[[NSUserDefaults standardUserDefaults] setInteger:A forKey:@"save_Interger_Value_For_Formula_One"];
}
NSInteger B = [[NSUserDefaults standardUserDefaults] integerForKey:@"save_Interger_Value_For_Formula_One"];
B = A;
NSLog(@" B is: %d",B);
B = A;
NSLog(@" A is: %d",A);
}
非常欢迎任何想法或建议。
Any Idea or suggestions would be highly welcome.
推荐答案
尝试这样实现..确保它会帮助...
Try to implement like this..Sure it'll help...
@implementation ClassA
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSUserDefaults standardUserDefaults]setInteger:100 forKey:@"save_Interger_Value_For_Formula_One"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
@end
@implementation ClassB
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
int B = [[[NSUserDefaults standardUserDefaults] integerForKey:@"save_Interger_Value_For_Formula_One"] integerValue];
NSLog(@"Interger Value %d",b);
}
这篇关于如何将整数值保存到NSuserDefault,如sqlite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文