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>
對不起,長職位,但希望要的一切。 對此的任何幫助將是偉大的。
嗨Jlehr,謝謝你,簡單的事情幫助這麼多!最後還有一件事,我該如何添加到數組中,而不是替換?目前,當添加新項目時,它將覆蓋當前項目? – jimbo 2010-09-15 07:59:44
我已經設法解決了這個問題,通過先讀取數據,然後添加新項目(addObject)到它的工作... – jimbo 2010-09-15 13:33:26
嗨吉姆博士,對不起,我沒有看到這一點。沒錯,要將對象添加到數組的末尾,可以使用'-addObject:';如果你想插入數組的其他地方,使用'-insertObject:atIndex:'。 – jlehr 2010-09-15 20:34:45