2012-01-16 97 views
2

我们需要能够读取现有plist文件的内容,然后添加到数组/字典中,然后将这些新数据写入plist文件。狭窄很简单。我们需要多个根键作为数组,每个键都有一些值。结构看起来像这样,将数据附加到PLIST - iPhone

124818240124810284.JPG   Array 
       item 0   String  Some Attribute 1 
       item 1   String  Some Attribute 2 

到目前为止,我们可以创建上述没有问题。但是,一旦我们写入另一个数组到plist,文件就会被覆盖,所有当前内容都将丢失。我们需要做的是在上面阅读,并添加到它,所以我们得到这样的事情,

124818240124810284.JPG   Array 
       item 0   String  Some Attribute 1 
       item 1   String  Some Attribute 2 
354273577823597297.JPG   Array 
       item 0   String  Some Attribute 1 
       item 1   String  Some Attribute 2 

等等。我们目前处于亏损状态,并且一直在为此奋斗一天。请尽可能帮助!提前致谢!!

目前,我们正在写这个文件如下

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Gallery.plist"]; 

// set the variables to the values in the text fields 
NSMutableArray *myPhotos = [[NSMutableArray alloc] initWithCapacity:2]; 
[myPhotos addObject:@"NO"]; 
[myPhotos addObject:@"NO"]; 


// create dictionary with values in UITextFields 
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: myPhotos, nil] forKeys:[NSArray arrayWithObjects: self.photoName, nil]]; 

NSString *error = nil; 

// create NSData from dictionary 
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; 

// check is plistData exists 
if(plistData) { 
    // write plistData to our Data.plist file 
     [plistData writeToFile:plistPath atomically:YES]; 
}else{ 
     NSLog(@"Error in saveData: %@", error); 
     [error release]; 
} 

下面就是我们结束了

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Gallery.plist"]; 

// Get current content. 
NSDictionary *oldContent = [NSDictionary dictionaryWithContentsOfFile:plistPath]; 
// Make a mutable copy. 
NSMutableDictionary *newContent = [[oldContent mutableCopy] autorelease]; 
// Add new stuff. 

NSMutableArray *myPhotos = [[NSMutableArray alloc] initWithCapacity:2]; 
[myPhotos addObject:@"Attribute 1"]; 
[myPhotos addObject:@"Attribute 2"]; 

[newContent setObject:myPhotos forKey:self.filePath]; 

// Now, write the plist: 
[newContent writeToFile:plistPath atomically:YES]; 
+0

因此,实质上你有麻烦添加另一个条目到数组?伪代码:'array = [self readPlist]; array = [array arrayByAppendingObject:newEntry]; [self writePlist:array];'那是你想要做的吗? – DarkDust 2012-01-16 18:01:40

+0

本质上是的,但我们在创建可变数组时遇到了麻烦,增加了数据然后写出来。我想发布我们目前如何编写文件,如果这将有助于 – mejim707 2012-01-16 18:08:51

+0

是的,请编辑您的问题,并发布导致麻烦的代码。 – DarkDust 2012-01-16 18:16:47

回答

2

你不喜欢这样:

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

没有在这里需要使用NSPropertyListSerialization

+0

这工作!我会在我的问题中发布我最终完成的代码。非常感谢你和每个人的及时回复! – mejim707 2012-01-16 19:08:22

-1

你有一个解释,首先,

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; 

等等.....为了增加它创建新的价值/密钥对一个新的字典:

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

创建一个新的字典:

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

或者将数据以现有的字典追加:

[dictionaryToAppend setValue:newAttribues ForKey:xxxxx.jpg"];