0

我正在使用 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。

非常感谢帮助。

问候德瓦斯基。

4

1 回答 1

3

plist解决了在运行时在上述结构上插入新记录的问题。

第一步:头文件声明。

   NSMutableDictionary *typeCategorySettings;
   NSArray  *typeCategory;
   NSMutableArray *typeValues;

第 2 步:将新记录从文本字段保存/写入 plist 文件。最后一行之前的一条记录

   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);` 

第 3 步:从 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;
于 2011-03-17T07:03:43.737 回答