solarissf
solarissf

Reputation: 1277

NSCoding - saving array to file or nsdefaults

Afternoon all,

Working on my first iphone app. I am trying to save an array of an array either to file or nsuserdefaults.
Data is like this...

MainArray (contains 3 below arrays) Array1 (contains 3 strings) Array2 (contains 3 strings) Array3 (contains 3 strings)

So far I've been reading about saving things to nsuserdefaults, and saving to file. Not sure which is the right way or benefits of either but I start trying saving to file.

below is my custom object to save information.

@implementation UserSettingsClass


+ (instancetype)sharedUserData{
static id sharedInstance = nil;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    sharedInstance = [self loadInstance];
    //sharedInstance = [[self alloc] init];
});

return sharedInstance;
    }


 -(void)encodeWithCoder:(NSCoder *)encoder{
[encoder encodeObject:self.arrayUserSettings forKey:@"someArray"];
[encoder encodeObject:self.userDescription forKey:@"testDesc"];
}


 - (id)initWithCoder:(NSCoder *)decoder{
if ((self = [super init])){
    self.userDescription = [decoder decodeObjectForKey:@"testDesc"];
    self.arrayUserSettings = [decoder decodeObjectForKey:@"someArray"];
}
return self;
}

+(NSString *)filePath{
static NSString *filePath = nil;
if (!filePath){
    filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"gamedata"];
}
return filePath;
}

+(instancetype)loadInstance{
NSData *decodedData = [NSData dataWithContentsOfFile:[UserSettingsClass filePath]];
if (decodedData){
    UserSettingsClass *gameData = [NSKeyedUnarchiver unarchiveObjectWithData:decodedData];
    return gameData;
}
return [[UserSettingsClass alloc] init];
}


-(void)save{
NSData *encodedData = [NSKeyedArchiver archivedDataWithRootObject:self];
[encodedData writeToFile:[UserSettingsClass filePath] atomically:YES];
}

and here is the main class where I am trying to use it.

    //initialize variables
//_userArray = [[NSMutableArray alloc] init];
//_userDescription = [[NSString stringWithFormat:@"testDescription"] init];
//_userLoginID = [[NSString stringWithFormat:@"testLogin"] init];
//_userPW = [[NSString stringWithFormat:@"testPassword"] init];
//    [_userArray addObject:[UserSettingsClass sharedUserData].userDescription];
//[_userArray addObject:_userLoginID];

//[[UserSettingsClass sharedUserData].arrayUserSettings addObject:           [UserSettingsClass sharedUserData].userDescription];

NSMutableArray *tempArray = [UserSettingsClass    sharedUserData].arrayUserSettings;

//[_userArray addObject:_userPW];

//save data to shared singleton class
//[[UserSettingsClass sharedUserData].arrayUserSettings addObject:_userArray];

//NSMutableArray *tempArray = [[NSMutableArray alloc] init];
//tempArray = [UserSettingsClass sharedUserData].arrayUserSettings;

//[UserSettingsClass sharedUserData].highScore = 10;
//int i = [UserSettingsClass sharedUserData].highScore;

//[UserSettingsClass sharedUserData].userDescription = @"hello";
NSString *temp2 = [UserSettingsClass sharedUserData].userDescription;

I am able to save the single string, but I must be doing something wrong. The single string I saved was just to see if I can get it working. My goal is to save the main array to file (or nsuserdefaults), which contain about 3 objects (array)... and each of those arrays contains 3 strings each.

any I doing something blatantly wrong?

Upvotes: 1

Views: 127

Answers (1)

zaph
zaph

Reputation: 112857

You are trying to hard.

If what you want to save is just NSArrays and NSStrings to you do not need so add an NSCoding, these types already conform to NSCoding. Just Archive to a file or "shudder" save to NSUserDefaults "/shudder".

It is really better to create a Data Model class and use NSArchiver to save and restore from a file in the Documents directory.

Upvotes: 2

Related Questions