首页 > ios > iOS对plist文件进行读写

iOS对plist文件进行读写

把主要的代码先复制过来……
读Plist文件

  NSString *errorDesc = nil;
  NSPropertyListFormat format;
  NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  NSString *plistPath = [rootPath stringByAppendingPathComponent:@"bookmarks.plist"];
  if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
  plistPath = [[NSBundle mainBundle] pathForResource:@"bookmarks" ofType:@"plist"];
  }
  NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
  context.bookmarksDic = (NSMutableDictionary *)[[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves
  format:&format
  errorDescription:&errorDesc] retain];
  keysArr = [[[context.bookmarksDic allKeys] sortedArrayUsingSelector:@selector(compare:)] retain];
  if (!context.bookmarksDic) {
  NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
  }

  写入Plist

  Context *context = [Context getInstance];
  NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
  NSUserDomainMask, YES) objectAtIndex:0];
  NSString *plistPath = [rootPath stringByAppendingPathComponent:@"bookmarks.plist"];
  NSString *error;
  NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:context.bookmarksDic
  format:NSPropertyListXMLFormat_v1_0
  errorDescription:&error];
  if(plistData) {
  [plistData writeToFile:plistPath atomically:YES];
  }
  else {
  NSLog(@"%@",error);
  [error release];
  }
  1. Gener 11月 7th, 2012 @ 19:56 | #1

    Thanks for that! It’s just the asnwer I needed.

评论提交中, 请稍候...

留言


可以使用的标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Trackbacks & Pingbacks ( 0 )
  1. 还没有 trackbacks