我将用户信息保存在 NSMutableDictionary 中,键为“用户名”,然后将此字典保存在另一个 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];
}
}