iphone - 如何像sqlite一样将整数值保存到NSuserDefault?

标签 iphone ios objective-c nsuserdefaults

我想在 NSUserDefault 中保存和检索值。我正在成功地保存和检索值(value)。但是当我去 cak Homepage 并再次进入我的类(class)时,值不会被存储,所有的过程都会再次完成。它不记住旧值。

我在做这个-

-(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);
}

我们非常欢迎任何想法或建议。

最佳答案

保存一个整数;

[[NSUserDefaults standardUserDefaults] setInteger:number forKey:@"unique_name"];
[[NSUserDefaults standardUserDefaults] synchronize];

取回它;

NSInteger x = [[NSUserDefaults standardUserDefaults] integerForKey:@"unique_name"];

祝你好运!

关于iphone - 如何像sqlite一样将整数值保存到NSuserDefault?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17647767/

相关文章:

ios - 在 UITableView 中显示 Json 结果

iphone - 将数组从 Controller 传递到 View

ios - Interface Builder 中的原型(prototype) ≠ UITableView 标题单元格的运行时结果

iphone - Xcode 3.2.6 适合运行 iOS 5 beta 6 的 iPhone 吗?

ios - 移动背景在 Spritekit 上停止重复

ios - 没有箭头的 UIPopoverPresentation

objective-c - 拖出 NSTableView 以移除

ios - NSData 和 UIImage 未在 iOS7 中发布

ios - iphone os 5 - 发送到实例的无法识别的选择器

iphone - TableView 中的复选框单元格 : User can't check it