我正在用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 :(得分: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];
}