iPhone plist向数组添加新字符串
我正在用plist文件中的值填充选择器。 plist的格式如下所示iPhone plist向数组添加新字符串,iphone,nsmutablearray,plist,uipicker,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&l
<?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>
在中调用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>
类型
AAAA
BBBBB
CCCCC
DDD
nnnn
使用模拟器和设备进行测试。但向现有数组中添加新字符串不会提交到plist文件
控制台报告如下:
数据文件:文件打开错误:
/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]路径:ddd
有什么建议可以解决向数组中添加字符串的问题吗。。。以及任何简单的解决方案为选择器。选项在运行时添加值。来自核心数据或plist
非常感谢您的帮助
关于
Devaski。解决了在上述
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;
解决了在上述
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;
我建议您参考此链接:您可以在plist上找到完整的解决方案。我建议您参考此链接:您可以在plist上找到完整的解决方案。