iPhone plist将新字符串添加到数组中

时间:2011-03-03 17:45:35

标签: iphone nsmutablearray plist uipicker

我正在使用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来为picker添加更多值。 使用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。

非常感谢帮助。

关心 Devaski。

1 个答案:

答案 0 :(得分:3)

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

第1步:标头文件声明。

   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;