且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-30 20:14:04

解决了在运行时在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;