iPhone plist将新字符串添加到数组 [英] iPhone plist add new string to array

查看:96
本文介绍了iPhone plist将新字符串添加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用plist文件中的值填充选择器. plist的格式如下.

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>

在将这个字典/数组调用到Picker的项目中,这可以正常工作.

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;

按如下所示调用self.typeValues

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];
}

但是,此外,我添加了新的modalViewcontroller来为选择器添加更多值. 使用来自modalviewcontroller的textField.text

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];

上述方法的目的是在一个Single Array中添加"n"个新行/字符串项. 像下面一样

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>

已通过模拟器和设备测试.但是,将新字符串添加到现有数组中并不会提交给plist文件.

Tested with simulator and device . But Adding new string to the existing array not getting committed to plist file.

控制台报告在这里:

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

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

是否有解决此plist并将字符串添加到数组的建议? ...以及任何简单的选择器解决方案.选择在运行中添加值.来自Core Data或plist.

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.

不胜感激.

问候 德瓦斯基.

推荐答案

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

Solved inserting new record at runtime on above plist structure.

第1步:标头文件声明.

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

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

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

步骤3:从Plist读取阵列.

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;

这篇关于iPhone plist将新字符串添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆