Appending data to PLIST - iPhone

前端 未结 2 1993
南笙
南笙 2020-12-20 04:10

we need to be able to read in the contents of an existing plist file then add to the array / dictionary then write this new data to the plist file. The stricture is simple.

相关标签:
2条回答
  • 2020-12-20 04:41

    You do it like this:

    // Get current content.
    NSDictionary *oldContent = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    // Make a mutable copy.
    NSMutableDictionary *newContent = [[oldContent mutableCopy] autorelease];
    // Add new stuff.
    [newContent setObject:newObject forKey:newKey];
    // Now, write the plist:
    [newContent writeToFile:plistPath atomically:YES];
    

    There is no need to use NSPropertyListSerialization at all here.

    0 讨论(0)
  • 2020-12-20 04:57

    You have a Dictionary to begin with,

    124818240124810284.JPG //[valueForKey: 124818240124810284.JPG] 
    
    
    
                        item 0          String     Some Attribute 1 //[valueForKey:124818240124810284.JPG] valueForKey:Item 0];
                        item 1          String     Some Attribute 2 //[valueForKey:124818240124810284.JPG] valueForKey:Item 1];
    
    
        354273577823597297.JPG    //[valueForKey: 354273577823597297.JPG]    
                        item 0          String     Some Attribute 1 //[valueForKey: 354273577823597297.JPG] valueForKey:Item 0];
                        item 1          String     Some Attribute 2 //[valueForKey: 354273577823597297.JPG] valueForKey:Item 1;
    

    And so on.....To add to it create a new dictionary with your new value/key pairs :

     NSMutableDictionary *newAttributes = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Attribute 0",@"Item 0",nil];
    

    Make a new dictionary:

       NSMutableDictionary *newEntry = [[NSMutableDictionary alloc]initWithObjectsAndKeys:newAttributes,@"xxxxx.jpg", nil];//Makes a new dictionary
    

    Or to append the data to the existing dictionary:

    [dictionaryToAppend setValue:newAttribues ForKey:xxxxx.jpg"];
    
    0 讨论(0)
提交回复
热议问题