2015-04-06 82 views
0

我正在向alist写plist数据返回我的nill值..我必须将数据追加到前面的数组值。但是价值观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]; 
} 
+0

即使是第一次,你的数组总是零吗?或者,它在“if”块或“else”块中返回nil的位置? – 2015-04-06 04:44:21

+0

alwys在IF条件下返回 – user3226440 2015-04-06 17:23:50

回答

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

它是否有区别...方法中的数组声明...或全局定义在.h中 – user3226440 2015-04-06 17:23:27