写入/将数据附加到Plist返回nil

时间:2015-04-06 04:23:33

标签: ios ios8 plist

我正在用alwys写数据到plist返回我的nill值..我还要将数据附加到数组的先前值。但是价值观是空的。 以下是代码:

 NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"a.plist"];

// set the variables to the values in the text fields


// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObject:@"c" forKey:@"Id"];


if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
    array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    [array addObject:plistDict];

    [array writeToFile:plistPath atomically:YES];


            NSLog(@"array%@",array);

}
else
{
    NSArray *array = [NSArray arrayWithObject:plistPath];
    [array writeToFile:plistPath atomically:YES];
}

1 个答案:

答案 0 :(得分:0)

如果我正确地理解了你的问题,我只用了一点改变来测试代码,即添加了数组声明,它工作正常。如果这不是问题,请原谅我。

 NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    // get documents path
    NSString *documentsPath = [paths objectAtIndex:0];
    // get the path to our Data/plist file
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"a.plist"];

    // set the variables to the values in the text fields
    NSMutableArray *array;

    // create dictionary with values in UITextFields
    NSDictionary *plistDict = [NSDictionary dictionaryWithObject:@"c" forKey:@"Id"];


    if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath])
    {
        array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
        [array addObject:plistDict];

        [array writeToFile:plistPath atomically:YES];


        NSLog(@"array%@",array);

    }
    else
    {
        NSArray *array = [NSArray arrayWithObject:plistPath];
        [array writeToFile:plistPath atomically:YES];
    }