Ios 向Plist写入/追加数据将返回nill

Ios 向Plist写入/追加数据将返回nill,ios,ios8,plist,Ios,Ios8,Plist,我正在向plist写入数据,并始终返回nil值。。我还必须将数据附加到数组的先前值。但是值总是空的。。 代码如下: NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); // get documents path NSString *documentsPath = [paths objectAtIndex:0]; // get the path to

我正在向plist写入数据,并始终返回nil值。。我还必须将数据附加到数组的先前值。但是值总是空的。。 代码如下:

 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];
}

如果我正确理解了您的问题,我只做了一点小小的更改就测试了代码,即添加了数组声明,它工作得很好。如果不是这个问题,请原谅

 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];
    }

您的数组是否始终为零,即使是第一次?或者在if块或else块中它在哪里返回nil?在if条件中alwys returm是否有区别…方法中的数组声明。。。或在.h中全局定义