当前位置: 代码迷 >> Iphone >> iPhone 对Plist资料进行读写
  详细解决方案

iPhone 对Plist资料进行读写

热度:23   发布时间:2016-04-25 06:22:19.0
iPhone 对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];  }
?

?

  相关解决方案