问题
I am populating a picker with values from a plist file. the format of plist is as below.
<?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">
<dict>
<key>type</key>
<array>
<string>AAAA</string>
<string>BBBBB</string>
<string>CCCCC</string>
</array>
</dict>
</plist>
In project calling this dictionary/array to a Picker, this works fine.
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"typecategory" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.typeCategorySettings = dictionary;
[dictionary release];
NSArray *typeComponents = [self.typeCategorySettings allKeys];
NSArray *sorted = [typeComponents sortedArrayUsingSelector:@selector(compare:)];
self.typeCategory = sorted;
NSString *selectedTypeCategory = [self.typeCategory objectAtIndex:0];
NSArray *array = [typeCategorySettings objectForKey:selectedTypeCategory];
self.typeValues = array;
Calling self.typeValues in as below
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.typeValues count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.typeValues objectAtIndex:row];
}
But, Additionally, I have added new modalViewcontroller to add more values for picker. With textField.text from modalviewcontroller
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:@"%@/typecategory.plist", documentsDirectory];
NSLog(@"path: %@", path);
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:path];
//self.typeCategorySettings = dictionary;
//[dictionary release];
NSArray *typeComponents = [dictionary allKeys];
NSArray *sorted = [typeComponents sortedArrayUsingSelector:@selector(compare:)];
//self.typeCategory = sorted;
NSString *selectedTypeCategory = [sorted objectAtIndex:0];
NSMutableArray *array = [dictionary objectForKey:selectedTypeCategory];
NSString *plistt = textField.text;
NSLog(@"path: %@", plistt);
[array addObject:plistt];
[array writeToFile:path atomically:YES];
Purpose of above method is to add "n" number of new rows/string items within one Single Array. like below
<?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">
<dict>
<key>type</key>
<array>
<string>AAAA</string>
<string>BBBBB</string>
<string>CCCCC</string>
<string>DDDDD</string>
<string>NNNNN</string>
</array>
</dict>
</plist>
Tested with simulator and device . But Adding new string to the existing array not getting committed to plist file.
Console report is here:
DataFile: file open error: /var/mobile/Library/Keyboard/en_GB-dynamic-text.dat, (Permission denied) 2011-03-03 22:49:05.028 TesApp[1562:307] path: /var/mobile/Applications/05074277-7E4D-4BC0-9CFA-XXXXXXXX/Documents/typecategory.plist 2011-03-03 22:49:05.031 TesApp[1562:307] path: DDDDD
Any suggestion to solve this plist adding string to array? ... as well any simple solution for picker. Option to add values on the run. Either from Core Data or plist.
Would appreciate help.
With Regards Devaski.
回答1:
Solved inserting new record at runtime on above plist
structure.
Step 1: Header file declarations.
NSMutableDictionary *typeCategorySettings;
NSArray *typeCategory;
NSMutableArray *typeValues;
Step 2: Save/write new record to plist file from textfield.. One record before last row
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"typecategory.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath]) //4
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"typecategory" ofType:@"plist"]; //5
[fileManager copyItemAtPath:bundle toPath:filePath error:&error]; //6
}
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
self.typeCategorySettings = dictionary;
NSLog(@"Dictionary Read%@", self.typeCategorySettings);
//[dictionary release];
NSArray *typeComponents = [self.typeCategorySettings allKeys];
NSMutableArray *sorted = [typeComponents sortedArrayUsingSelector:@selector(compare:)];
self.typeCategory = sorted;
NSLog(@"Array Read%@", self.typeCategory);
NSString *selectedTypeCategory = [self.typeCategory objectAtIndex:0];
NSMutableArray *array = [typeCategorySettings objectForKey:selectedTypeCategory];
self.typeValues = array;
NSLog(@"Values Read%@", self.typeValues);
NSString *test;
test = textField.text;
[array insertObject:test atIndex:([self.typeValues count]-1)];
[dictionary setObject:array forKey:@"type"];
[dictionary writeToFile:filePath atomically:YES];
NSLog(@"Written Read%@", dictionary);`
Step 3: Reading the Array from Plist.
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"typecategory.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:plistPath]) //4
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"typecategory" ofType:@"plist"]; //5
[fileManager copyItemAtPath:bundle toPath:plistPath error:&error]; //6
}
//NSBundle *bundle = [NSBundle mainBundle];
//NSString *plistPath = [bundle pathForResource:@"typecategory" ofType:@"plist"];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
self.typeCategorySettings = dictionary;
NSArray *typeComponents = [self.typeCategorySettings allKeys];
NSArray *sorted = [typeComponents sortedArrayUsingSelector:@selector(compare:)];
self.typeCategory = sorted;
NSString *selectedTypeCategory = [self.typeCategory objectAtIndex:0];
NSMutableArray *array = [typeCategorySettings objectForKey:selectedTypeCategory];
self.typeValues = array;
来源:https://stackoverflow.com/questions/5184428/iphone-plist-add-new-string-to-array