我保存用戶信息在關鍵「用戶名」的NSMutableDictionary,然後保存在另一個NSMutableDictionary與關鍵字「名稱」,然後將該字典保存在NSMutableArray中的字典。但問題是,只要我保存另一個用戶使用不同的用戶名,它會覆蓋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];
}
}
然後請顯示正確的代碼。 – trojanfoe
我用代碼更新了我的答案。 – Saleh