好的,我一直有问题覆盖我创建的plist的内容。我已经包含了以下所有代码:
因此,在我的第一个视图控制器中,我创建了plist,然后使用update目录方法更新它。好的,我的问题出现在覆盖plist区域。我的想法是从文件创建一个字典,然后添加/覆盖某些键,然后重新写入该文件。这段代码允许我编写没有错误的对象,但是当我尝试从文件中读取值时,我对所有新值都获取null。
更新:代码现在有效。我需要将Area _#_ Total_L更改为Area _#_ Total_L.text。这是因为可以添加到字典中的项目数量有限(请参阅对象类型的答案)。
创建目录:
-(void)Create_Directory {
// Create File Location
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Temporary_Variable_Repository.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Temporary_Variable_Repository" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
// End
NSLog(@"path in ViewController: %@", path);
DataLocation = path;
}
更新目录:
-(void)Update_Directory {
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: DataLocation];
//All Variables that need to be saved need to be listed here
//Creating Directory Example
//[data setObject:@"Directory" forKey:@"Key"];
NSLog(@"Saving Variables...");
int Temp = [[data valueForKey:@"-Run_ID"] intValue]+1;
NSLog(@"Run ID: %d", Temp);
[data setObject:[NSNumber numberWithInt:Temp] forKey:@"-Run_ID"];
[data setObject:Date_Time forKey:@"-Run_Date_Time"];
[data setObject:@"In Progress" forKey:@"-Run_Status"];
[data setObject:@"ViewController" forKey:@"-Run_Chapter"];
[data setObject:@"0" forKey:@"-Run_Page"];
[data setObject:@"User" forKey:@"-User"];
[data setObject:@"0001" forKey:@"-User_Device"];
//End
BOOL result = [data writeToFile: DataLocation atomically:YES];
NSLog(result ? @"Yes" : @"No");
}
覆盖Plist
NSLog(@"Saving Variables...");
//Verify the variables are updated prior to transfer
[self RefreshAreaLabels];
//Close keypad
[self closeKeypad];
//Prep next screen for loading
RoofSlope *NewView = [[RoofSlope alloc] initWithStyle:UITableViewStylePlain];
//Send location of Temp Var Rep
NewView.DataLocation_Pass2 = DataLocation_Pass1;
//Create a copy of Temp Var Rep
NSMutableDictionary *ViewController_Reference = [[NSMutableDictionary alloc] initWithContentsOfFile:DataLocation_Pass1];
//Write variables
[ViewController_Reference setObject:@"AtticArea" forKey:@"-Run_Chapter"];
[ViewController_Reference setObject:[NSString stringWithFormat:@"%@", Area_1_Length_TF.text] forKey:@"Area_1_Length"];
[ViewController_Reference setObject:Area_1_Width_TF.text forKey:@"Area_1_Width"];
[ViewController_Reference setObject:Area_1_Total_L.text forKey:@"Area_1_Total"];
[ViewController_Reference setObject:Area_2_Length_TF.text forKey:@"Area_2_Length"];
[ViewController_Reference setObject:Area_2_Width_TF.text forKey:@"Area_2_Width"];
[ViewController_Reference setObject:Area_2_Total_L.text forKey:@"Area_2_Total"];
[ViewController_Reference setObject:Area_3_Length_TF.text forKey:@"Area_3_Length"];
[ViewController_Reference setObject:Area_3_Width_TF.text forKey:@"Area_3_Width"];
[ViewController_Reference setObject:Area_3_Total_L.text forKey:@"Area_3_Total"];
[ViewController_Reference setObject:Area_4_Length_TF.text forKey:@"Area_4_Length"];
[ViewController_Reference setObject:Area_4_Width_TF.text forKey:@"Area_4_Width"];
[ViewController_Reference setObject:Area_4_Total_L.text forKey:@"Area_4_Total"];
[ViewController_Reference setObject:@"TEST" forKey:@"-Run_ID"];
[ViewController_Reference setObject:[NSNumber numberWithDouble:[TotalAreaValue_L.text doubleValue]] forKey:@"Total_Area"];
//Debug
NSLog(@"Area_1_Length: %@", [ViewController_Reference valueForKey:@"Area_1_Length"]);
NSLog(@"_Run_ID: %@", [ViewController_Reference valueForKey:@"-Run_ID"]);
//Update Variable Repository
//This writes the updated variables to the plist
BOOL result = [ViewController_Reference writeToFile: DataLocation_Pass1 atomically:YES];
NSLog(result ? @"Yes" : @"No");
答案 0 :(得分:0)
作为苹果文件:
如果数组的内容都是属性列表对象(NSString, NSData,NSArray或NSDictionary对象),由此编写的文件 方法可用于使用类方法初始化新数组 arrayWithContentsOfFile:或实例方法 initWithContentsOfFile :.此方法递归验证所有 在写出之前,包含的对象是属性列表对象 file,如果所有对象都不是属性列表对象,则返回NO, 因为结果文件不是有效的属性列表。
确保所有对象都是属性列表对象(NSString,NSData,NSArray或NSDictionary对象)。
[NSNumber numberWithInt:Temp]
可能是问题。