简体   繁体   中英

saving in NSUserDefaults

I'm having a problem on how/where I can save my NSUserDefaults for applicationDidEnterBackground and applicationWillTerminate , I need to save my NSArray count in NSUserDefaults from another UIViewController when user exits the app or enter background. But I don't know if I would save the right integer , because my NSArray count is being updated when I am deleting on it on UIView . I cant save it on viewDidLoad and viewDidAppear . Hope someone would help. Thankyou.

If u need to save from another ViewController then its better u add NSMutableArray in appDelegate. Whenever you delete or add object then save the count of array as it will overwrite like this:

 [[NSUserDefaults standardUserDefaults] setInteger:[yourArray count] forKey:@"Count"];
 [[NSUserDefaults standardUserDefaults] synchronize];

Retrieve like this:

 NSInteger count = [[NSUserDefaults standardUserDefaults] integerForKey:@"Count"];

EDIT : synchronize : Writes any modifications to the persistent domains to disk and updates all unmodified persistent domains to what is on disk.

Refer NSUserDefaults_Class as clearly states gets called perodically and we don't have to wait for their call

Create the required NSMutableArray in your appDelegate class ie create a property of NSMutableArray iVar,then later you can update ,add ,delete contents from it in any of the classes.Thus you shall get the array count in the applicationDidEnterBackground and applicationWillTerminate notification functions in the appDelegate function itself and the array count should be accurate..

in yourAppDelegate.h

create ..

    @interface yourAppDelegate : UIResponder <UIApplicationDelegate>{
    NSMutableArray *myArray;
    }
@property(nonatomic,strong)NSMutableArray *myArray; //replace strong by retain if ios <5

in yourAppDelegate.m

@synthesize myArray;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *
)launchOptions{
     self.myArray = [NSMutableArray array];
}

further in your required functions in appDelegate.m file

- (void)applicationWillTerminate:(UIApplication *)application
{
[[NSUserDefaults standardDefaults]setValue:[NSNumber numberWithInt:[self.myArray count] forKey:@"myOwnKey"]];
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

in case if you want to add objects to this array in any other class just create a delegate of the yourAppDelegate .

Eg in viewDidLoad of class A,

-(void)viewDidLoad{  // assuming viewController A

YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplicatio sharedApplication]delegate];
[appDelegate.myArray addObject:@"1232"];
}

You can use this code to save value to NSUserDefaults

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:yourArray forKey:@"YourKey"]
[defaults synchronize];

For details on using NSUserDefault check this

  • [[NSUserDefaults standardUserDefaults] setBool:<#(BOOL)#> forKey:<#(NSString *)#>] , this Is for Boolean you an add setInt/setFloat/SetDouble etc and even ObjectiveC objects with setValue/setObject .

     [[NSUserDefaults standardUserDefaults] setValue:<#(id)#> forKey:<#(NSString *)#>]; [[NSUserDefaults standardUserDefaults] setObject:<#(id)#> forKey:<#(NSString *)#>]; 
  • To retrieve the value you can use getters like boolForKey ( [[NSUserDefaults standardUserDefaults] boolForKey:<#(NSString *)#>] ) intForKey etc. .

  • Use removeObjectForKey to remove objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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