我编写了一些从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>
对于长篇文章感到抱歉,但希望应该是所有相关的内容。 对此的任何帮助都会很棒。
答案 0 :(得分: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];
}
道歉,我没有测试过上面的代码,但它至少应该用来说明这一点。