ユーザー情報をキー「username」でNSMutableDictionaryに保存してから、このディクショナリをキー「name」で別のNSMutableDictionaryに保存してから、ディクショナリをNSMutableArrayに保存しています。しかし、問題は、別のユーザー名で別のユーザーを保存すると、NSMutableArrayの値が上書きされることです。以下は私が使用しているコードです
-(void) saveUserData:(NSString *)username
{NSLog(@ "\ nsaveDataBarButtonTapped \ n");
NSLog(@"the count is %d",[[[ArraysManager sharedInstance] getTotalUserArray] count]);
NSLog(@"ArraysManager description is %@ ",[[[ArraysManager sharedInstance] getTotalUserArray] description]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directory = [paths objectAtIndex:0];
NSString *userArrayFilePath = [[NSString alloc] initWithString:[directory stringByAppendingPathComponent:@"userData"]];
if (username.length != 0)
{
[dataDictionary setObject:[NSString stringWithFormat:@"%@",username] forKey:@"username"];
[dataDictionary setObject:[NSString stringWithFormat:@"%lf",totalValue] forKey:@"totalValue"];
[dataDictionary setObject:[NSString stringWithFormat:@"%lf",totalPayable] forKey:@"totalPayable"];
[userDictionary setObject:dataDictionary forKey:@"userDictionary"];
[[[ArraysManager sharedInstance] getTotalUserArray] addObject:userDictionary];
[[[ArraysManager sharedInstance] getTotalUserArray] writeToFile:userArrayFilePath atomically:NO];
[dataDictionary removeAllObjects];
[userDictionary removeAllObjects];
NSLog(@"ArraysManager description is %@ ",[[[ArraysManager sharedInstance] getTotalUserArray] description]);
}
else
{
UIAlertView *noNameAlertView = [[UIAlertView alloc] initWithTitle:@"No Name Found"
message:@"Please Enter the User Name"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[noNameAlertView show];
[noNameAlertView release];
}
}