2012-08-05 60 views
0

你好那里我试图用电话号码创建一个字典,我可以填充字典并将其添加到plist,但是当我尝试再次添加到plist时,我覆盖文件。pList文件中的旧值被覆盖

#import "FirstViewController.h" 

@interface FirstViewController() 
{ 
    NSMutableDictionary *NameNumberDict; 
    NSDictionary *plistDict; 
    NSMutableArray *numberArray; 
    NSString *filePath; 
} 

@end 

@implementation FirstViewController 
@synthesize NameTxtField; 
@synthesize FirstNumField; 
@synthesize SecNumField; 
@synthesize personName; 
@synthesize phoneNumbers; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //get some memory 
    plistDict = [[NSDictionary alloc]init]; 
    NameNumberDict = [[NSMutableDictionary alloc]init]; 

    //make a file path string 
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *path = [pathArray objectAtIndex:0]; 
    filePath = [path stringByAppendingPathComponent:@"data.plist"]; 

    NSFileManager *manager = [NSFileManager defaultManager]; 

    // here i set the Dictionary to the file 

    //give the file to my dictonary 
    NameNumberDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 
    NSLog(@"The count: %i", [NameNumberDict count]); 

    //make sure the file is there 
    NSString *err = nil; 
    NSData *plist; 
    plist = [NSPropertyListSerialization dataFromPropertyList:NameNumberDict format:NSPropertyListBinaryFormat_v1_0 errorDescription:&err]; 

    if([manager fileExistsAtPath:@"data.plist"] == NO) 
    { 
     [manager createFileAtPath:[NSString stringWithFormat:@"data.plist"] contents:plist attributes:nil]; 
    } 
} 

- (void)viewDidUnload 
{ 
    [self setNameTxtField:nil]; 
    [self setFirstNumField:nil]; 
    [self setSecNumField:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

// Populate the dictionary when they dismiss the keyboard if all the data is filled out 

-(IBAction)TextFieldReturn:(id)TextField 
{ 
    if(!NameTxtField.text && !FirstNumField.text && !SecNumField.text); 
    { 
     NSString *name = NameTxtField.text; 

     numberArray = [[NSMutableArray alloc]init]; 
     [numberArray addObject:FirstNumField.text]; 
     [numberArray addObject:SecNumField.text]; 

     [NameNumberDict setObject:name forKey:@"Name"]; 
     [NameNumberDict setObject:numberArray forKey:@"Number"]; 

     NSLog(@"dicSave: %@",NameNumberDict); 
    } 

    [TextField resignFirstResponder]; 
} 

// and down here is where im lost Im not sure how to append the data to the Dictionary and write it to file 

- (IBAction)AddBtn:(UIButton *)sender 
{ 
    plistDict = [[NSMutableDictionary alloc]initWithDictionary:NameNumberDict]; 

    [plistDict writeToFile:filePath atomically:YES]; 

    NSLog (@"File %@ exists on iPhone",filePath); 
} 

@end 

回答

0

在你的“TextFieldReturn”的方法,我看你总是重置“NameNumberDict”可变字典的每它被称为一次“Name”和“Number”对象。

我认为你真正想要做的是将一个新的字典(对于每个新名字&数字)添加到一个可变数组中,然后将该可变数组写入一个文件(最终将成为属性列表文件) 。

而且,只是作为一种风格注:方法名和变量名应该总是以小写字母开头(如“textFieldReturn”,“addBtn”,“nameNumberDict”等),而类别名称(如“FirstViewController”)是什么开始资本化。