【发布时间】:2011-03-03 17:45:35
【问题描述】:
我正在使用 plist 文件中的值填充选择器。 plist的格式如下。
<?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>
在将这个字典/数组调用到 Picker 的项目中,这工作正常。
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;
如下调用self.typeValues
- (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];
}
但是,另外,我添加了新的 modalViewcontroller 来为选择器添加更多值。 使用 modalviewcontroller 中的 textField.text
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];
上述方法的目的是在一个单一数组中添加“n”个新行/字符串项。 如下所示
<?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>
用模拟器和设备测试。但是向现有数组添加新字符串不会提交到 plist 文件。
控制台报告在这里:
DataFile:文件打开错误: /var/mobile/Library/Keyboard/en_GB-dynamic-text.dat, (权限被拒绝) 2011-03-03 22:49:05.028 TesApp[1562:307] 路径: /var/mobile/Applications/05074277-7E4D-4BC0-9CFA-XXXXXXXX/Documents/typecategory.plist 2011-03-03 22:49:05.031 TesApp[1562:307] 路径:DDDDD
有什么建议可以解决这个将字符串添加到数组的 plist? ...以及任何简单的选择器解决方案。在运行中添加值的选项。来自 Core Data 或 plist。
不胜感激。
问候 德瓦斯基。
【问题讨论】:
-
我建议你参考这个链接:iphonesdevsdk.blogspot.com/2011/04/plist.html你可以在plist上找到完整的解决方案。
标签: iphone nsmutablearray plist uipicker