pList文件中的旧值被覆盖

时间:2012-08-05 18:24:30

标签: iphone 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

1 个答案:

答案 0 :(得分:0)

在“TextFieldReturn”方法中,我发现您始终重置“Name”和“Number”对象的“NameNumberDict”可变字典时间被召唤。

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

另外,就像样式注释:方法名称和变量名称应始终以小写字母开头(例如“textFieldReturn”,“addBtn”,“nameNumberDict”等。)虽然类名(例如“FirstViewController”)是开始大写的。