Public class: The best way to store and access NSMutableDictionary?
Posted
by meridimus
on Stack Overflow
See other posts from Stack Overflow
or by meridimus
Published on 2010-05-23T14:33:08Z
Indexed on
2010/05/23
14:40 UTC
Read the original article
Hit count: 274
I have a class to help me store persistent data across sessions. The problem is I want to store a running sample of the property list or "plist" file in an NSMutableArray throughout the instance of the Persistance class so I can read and edit the values and write them back when I need to.
The problem is, as the methods are publicly defined I cannot seem to access the declared NSMutableDictionary without errors. The particular error I get on compilation is:
warning: 'Persistence' may not respond to '+saveData'
So it kind of renders my entire process unusable until I work out this problem.
Here is my full persistence class (please note, it's unfinished so it's just to show this problem):
Persistence.h
#import <UIKit/UIKit.h>
#define kSaveFilename @"saveData.plist"
@interface Persistence : NSObject {
NSMutableDictionary *saveData;
}
@property (nonatomic, retain) NSMutableDictionary *saveData;
+ (NSString *)dataFilePath;
+ (NSDictionary *)getSaveWithCampaign:(NSUInteger)campaign andLevel:(NSUInteger)level;
+ (void)writeSaveWithCampaign:(NSUInteger)campaign andLevel:(NSUInteger)level withData:(NSDictionary *)saveData;
+ (NSString *)makeCampaign:(NSUInteger)campaign andLevelKey:(NSUInteger)level;
@end
Persistence.m
#import "Persistence.h"
@implementation Persistence
@synthesize saveData;
+ (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kSaveFilename];
}
+ (NSDictionary *)getSaveWithCampaign:(NSUInteger)campaign andLevel:(NSUInteger)level
{
NSString *filePath = [self dataFilePath];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSLog(@"File found");
[[self saveData] setDictionary:[[NSDictionary alloc] initWithContentsOfFile:filePath]]; // This is where the warning "warning: 'Persistence' may not respond to '+saveData'" occurs
NSString *campaignAndLevelKey = [self makeCampaign:campaign andLevelKey:level];
NSDictionary *campaignAndLevelData = [[self saveData] objectForKey:campaignAndLevelKey];
return campaignAndLevelData;
}
else
{
return nil;
}
}
+ (void)writeSaveWithCampaign:(NSUInteger)campaign andLevel:(NSUInteger)level withData:(NSDictionary *)saveData
{
NSString *campaignAndLevelKey = [self makeCampaign:campaign andLevelKey:level];
NSDictionary *saveDataWithKey = [[NSDictionary alloc] initWithObjectsAndKeys:saveData, campaignAndLevelKey, nil];
//[campaignAndLevelKey release];
[saveDataWithKey writeToFile:[self dataFilePath] atomically:YES];
}
+ (NSString *)makeCampaign:(NSUInteger)campaign andLevelKey:(NSUInteger)level
{
return [[NSString stringWithFormat:@"%d - ", campaign+1] stringByAppendingString:[NSString stringWithFormat:@"%d", level+1]];
}
@end
I call this class like any other, by including the header file in my desired location:
@import "Persistence.h"
Then I call the function itself like so:
NSDictionary *tempSaveData = [[NSDictionary alloc] [Persistence getSaveWithCampaign:currentCampaign andLevel:currentLevel]];
© Stack Overflow or respective owner