2010-09-14 63 views
1

我已經寫了一些從plist文件中提取信息的代碼,並且做了一些事情然後最終將這些數據彈出到表中。字典和plist文件

這一切工作正常,問題是我寫的代碼從資源文件夾中的plist文件拉數據,但實際的文件將通過操作創建並存儲在內存中。從新位置拉出代碼時,我需要一些幫助。

當前工作代碼:

- (void) loadData{ 
// Load items 
NSString *error; 
NSPropertyListFormat format; 
NSString *path = [[NSBundle mainBundle] pathForResource:@"CalculatorData" ofType:@"plist"]; 
NSData *plistData = [NSData dataWithContentsOfFile:path]; 
NSArray *amountData = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error]; 
NSDictionary *amountData = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error]; 

if (amountData) { 
    self.amounts = [[NSMutableArray alloc] initWithCapacity:[amountData count]]; 
    result = 0; 
    for (NSDictionary *amountsDictionary in amountData) { 

     [self.amounts addObject:amountsDictionary]; 

     currentNumber = [[amountsDictionary objectForKey:@"Value"] floatValue]; 

     if ([[amountsDictionary objectForKey:@"Type"] isEqualToString:@"Plus"]) { 
      result = result + currentNumber; 
     } else { 
      result = result - currentNumber; 
     } 
    } 
    //Set main amount 
    NSString *msg = [NSString stringWithFormat:@"£%.2f",result]; 
    [lblAmount setText:msg]; 
} else { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Why not keep track of your income and expensies? Add new items below." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 
} 

我認爲拉離新的文件位置的信息,我需要類似下面的東西,但似乎無法得到的輸出與上面相同:

//Get file location 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saveBudget.plist"]; 

// read it back in with different dictionary variable 
NSMutableDictionary *amountData = [NSMutableDictionary dictionaryWithContentsOfFile:path]; 
if(amountData==nil){ 
    NSLog(@"failed to retrieve dictionary from disk"); 
} else { 
    NSLog(@"%@", amountData); 
} 

創建plist文件的代碼(這也需要一點的工作,因爲它總是目前覆蓋了什麼做過,當我想它添加一個新的數組):

-(void) addData { 

// create a dictionary to store a fruit's characteristics 
NSMutableDictionary *items = [[NSMutableDictionary alloc] init]; 
[items setObject:@"10" forKey:@"Value"]; 
[items setObject:@"tester" forKey:@"Description"]; 
[items setObject:@"Plus" forKey:@"Type"]; 

// create a dictionary to store all fruits 
NSMutableDictionary *plist = [[NSMutableDictionary alloc] init]; 
[plist setObject:items forKey:@"0"]; 

//Get file location 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saveBudget.plist"]; 

// write plist to disk 
[plist writeToFile:path atomically:YES]; 
} 

這也需要一點的工作,因爲我想它是:

是涉及
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<array> 
    <dict> 
     <key>Date</key> 
     <string>27/9/10</string> 
     <key>Value</key> 
     <string>10.97</string> 
     <key>Description</key> 
     <string>PaperRound</string> 
     <key>Type</key> 
     <string>Plus</string> 
    </dict> 
    <dict> 
     <key>Date</key> 
     <string>27/9/10</string> 
     <key>Value</key> 
     <string>10.97</string> 
     <key>Description</key> 
     <string>PaperRound</string> 
     <key>Type</key> 
     <string>Plus</string> 
    </dict> 
    <dict> 
     <key>Date</key> 
     <string>27/9/10</string> 
     <key>Value</key> 
     <string>10.97</string> 
     <key>Description</key> 
     <string>PaperRound</string> 
     <key>Type</key> 
     <string>Plus</string> 
    </dict> 
    <dict> 
     <key>Date</key> 
     <string>27/9/10</string> 
     <key>Value</key> 
     <string>10.97</string> 
     <key>Description</key> 
     <string>PaperRound</string> 
     <key>Type</key> 
     <string>Plus</string> 
    </dict> 
</array> 
</plist> 

對不起,長職位,但希望要的一切。 對此的任何幫助將是偉大的。

回答

1

如果你想plist中的最外層元素是一個數組,然後在您發佈創建的錯誤類型的對象的代碼下面幾行:

// create a dictionary to store all fruits 
NSMutableDictionary *plist = [[NSMutableDictionary alloc] init]; 
[plist setObject:items forKey:@"0"]; 

我還要提防離開陳舊的您的代碼中的評論陳述,因爲它們有時可能會導致您誤導自己。這是你的addData方法的建議重寫:

-(void)addData { 

    // Create a dictionary representing an item 
    // 
    NSDictionary *item = [NSDictionary dictionaryWithObjectsAndKeys: 
          @"10", @"Value", 
          @"tester", @"Description", 
          @"Plus", @"Type", 
          nil]; 

    // Create an array of items. This must be an instance of NSArray or 
    // NSMutableArray if you want the corresponding element in your plist 
    // to be an array. 
    // 
    NSArray *items = [NSArray arrayWithObjects: item, nil]; 

    // Get file location (this part is fine). 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saveBudget.plist"]; 

    // Write array of items to filesystem 
    [items writeToFile:path atomically:YES]; 
} 

做出道歉,我沒有測試上面的代碼,但它應該至少可以說明這一點。

+0

嗨Jlehr,謝謝你,簡單的事情幫助這麼多!最後還有一件事,我該如何添加到數組中,而不是替換?目前,當添加新項目時,它將覆蓋當前項目? – jimbo 2010-09-15 07:59:44

+0

我已經設法解決了這個問題,通過先讀取數據,然後添加新項目(addObject)到它的工作... – jimbo 2010-09-15 13:33:26

+0

嗨吉姆博士,對不起,我沒有看到這一點。沒錯,要將對象添加到數組的末尾,可以使用'-addObject:';如果你想插入數組的其他地方,使用'-insertObject:atIndex:'。 – jlehr 2010-09-15 20:34:45