2011-03-24 82 views
1

我已經看到了很多關於這個問題,但似乎找不到一個適合我的需求。閱讀寫作plist Iphone

我有一個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>Content</key> 
    <array> 
     <dict> 
      <key>dateAdd</key> 
      <date>2011-03-23T14:40:47Z</date> 
      <key>content</key> 
      <string>Content first</string> 
      <key>Title</key> 
      <string>Title 1</string> 
     </dict> 
     <dict> 
      <key>dateAdd</key> 
      <date>2011-03-23T14:40:47Z</date> 
      <key>content</key> 
      <string>Content here</string> 
      <key>Title</key> 
      <string>Title 2</string> 
     </dict> 
    </array> 
</dict> 
</plist> 

林取出由這樣

plist中的信息在第一類中

//points to the plist 
    NSString *path = [[NSBundle mainBundle] bundlePath]; 
    NSString *url = [path stringByAppendingPathComponent:@"Details.plist"]; 

    //filling with contents of plist 
    NSDictionary *Data = [[NSDictionary alloc] initWithContentsOfFile:url]; 
    self.contentData = Data; 
    [Data release]; 

在第二類我創建此類的實例並獲取信息

Petite_NotesAppDelegate *instance = (Petite_NotesAppDelegate *)[[UIApplication sharedApplication] delegate]; 

self.dataArray = [instance.contentData objectForKey:@"Content"]; 

這使得有可能獲得從plist中這樣

NSDictionary *Content = [self.dataArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [Content objectForKey:@"Title"]; 

我的問題是我不能夠寫的plist

因爲我想寫的內容是在plist中的內容數組內容,然後在項目0項目1等內。我將如何寫入plist內容數組內?比方說,我想寫一個文本字段的內容...

感謝您的任何幫助!

+0

處理看看這有助於。 [PropertyLists/Introduction](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html#//apple_ref/doc/uid/10000048-CJBGDEGD) – 2011-03-24 09:36:18

回答

7

的問題是,你可能想寫信給你讀取相同的文件。恐怕這是不可能的 - 您正在閱讀應用程序包,這是只讀。如果您想要將更改保存到磁盤,則需要將您的字典寫入位於目錄中的plist。

要獲得的路徑,你需要做以下的文檔目錄(也有這樣做,以及其他方式):

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

// you will read and write to this path 
NSString *url = [documentsDirectory stringByAppendingPathComponent:@"Details.plist"]; 

這也意味着,當你啓動應用程序,你應該檢查查看Details.plist文件是否存在於您的文檔目錄中,如果有,請從中讀取,否則回退到應用程序包的讀取。

+0

謝謝很多都是這樣做的,我是從另外一個地方讀書寫給另一個地方的。 – Tobias 2011-03-26 20:31:38

0

您可以更改到字典中,然後用writeToFile:

Sample

0

這可以通過伊斯利陣列deepcopy的

-(void) WriteOverSamePlist : (NSNumber *) num { 
    NSString *appPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"storedRowIds.plist"]; 
    NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[self ReadArrayFromPlist] copyItems:YES]; //[[NSMutableArray alloc] initWithObjects:@"this", @"is" , nil]; 
    [arr addObject:num]; 
    BOOL fileExists = [arr writeToFile:appPath atomically:YES]; 

    if(fileExists) { 
     NSLog(@"successfully written"); 
    } else { 
     NSLog(@"failed to write"); 
    } 
} 


-(NSMutableArray*) ReadArrayFromPlist { 
    NSString *appPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"storedRowIds.plist"]; 
    NSMutableArray *coolArray = [NSMutableArray arrayWithContentsOfFile:appPath]; 
    return coolArray; 
}