Reputation: 75
My app in development is related to survey questions. (fun ones, not boring ones!) I want to create a tier system for each question relative to the user, each time they answer a specific question I want to associate a value to that question for that user, identifying how many times they've answered it.
I believe the way I need to achieve this is NSMutableDictionary
and NSUserDefaults
. This is a simplified version of my code:
NSMutableDictionary *questionTierDictionary = [[NSMutableDictionary alloc]init];
[[NSUserDefaults standardUserDefaults] objectForKey:@"questionTiers"];
[questionTierDictionary setObject:[NSNumber numberWithInt:4] forKey:@(2)];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"%@", questionTierDictionary);
Does this code save this data indefinitely to the app, or does it disappear once the user has closed the app? If so, do you have any suggestions on how I can easily test to see if the data was stored?
Upvotes: 1
Views: 65
Reputation: 234
sandbox path :
~~ Documents:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [paths objectAtIndex:0];
~~~ Caches:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachesDir = [paths objectAtIndex:0];
~~~ tmp:
NSString *tmpDir = NSTemporaryDirectory();
~~~ home sandbox:
NSString *homeDir = NSHomeDirectory();
~~~ for pic :
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"];
UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
Example:
NSFileManager* fm=[NSFileManager defaultManager];
if(![fm fileExistsAtPath:[self dataFilePath]]){
//
[fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];
//
NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];
//
NSData *data = [fm contentsAtPath:[self dataFilePath]];
//
NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];
}
I hope I could help you!
Upvotes: 1
Reputation: 561
NSUserDefaults
save data permanently in you application directory till you remove it manually... to save a object in NSUserDefaults
code like this
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"yourObject",@"yourKey", nil];
[[NSUserDefaults standardUserDefaults]setObject:dict forKey:@"dict"];
//fetch Like this
NSDictionary *dict1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"dict"];
Upvotes: 0