2014-02-27 85 views
0

好的,我一直有問題覆蓋我創建的plist的內容。我已經包含以下所有代碼:重寫plist的問題

因此,在我的第一個視圖控制器中,我創建了plist,然後使用update目錄方法更新它。好的,我的問題出現在覆蓋plist區域。我的想法是從文件創建一個字典,然後添加/覆蓋某些鍵然後重新寫入文件。這段代碼允許我編寫沒有錯誤的對象,但是當我嘗試讀取文件中的值時,我會爲所有新值得到空值。

更新:代碼現在工作。我需要將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

隨着應用樂文件:

如果陣列中的內容都是屬性列表對象(的NSString, 的NSData,NSArray中,或NSDictionary中的對象),通過 方法寫入的文件可以被用來初始化類方法的新數組 arrayWithContentsOfFile:或實例方法 initWithContentsOfFile :.在寫出 文件之前,此方法遞歸地驗證所有包含的對象是屬性列表對象,如果所有對象不是屬性列表對象,則返回NO,因爲生成的文件不是有效的屬性列表,所以它將返回NO 。

確保您的所有對象都是屬性列表對象(NSString,NSData,NSArray或NSDictionary對象)。

[NSNumber numberWithInt:Temp]可能是問題所在。

+0

我會驗證我的物品類型,但我認爲我的問題不在於「numberWithInt」。因爲該值通過NSLog顯示,並在兩個視圖控制器中都正確。我相信所有來自viewcontroller的值都會正確保存。我用NSLog語句檢查了它們。問題是當我添加新對象並試圖覆蓋新對象不顯示的文件時。相反,該文件仍然顯示在viewcontroller中設置的值。 – jdavid22

+0

好的更新解決了這個問題。你是對的....有點問題是當我試圖傳輸區域_#_ Total_L。問題是我試圖將標籤保存到字典中。相反,它需要區域_#_ Total_L.text感謝您的幫助。 – jdavid22