开发学院手机开发iPhone 开发 iPhone 对Plist文件进行读写 阅读

iPhone 对Plist文件进行读写

 2010-09-07 10:49:00 来源:WEB开发网   
核心提示:读Plist文件NSString *errorDesc = nil;NSPropertyListFormat format;NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, Y

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

}

Tags:iPhone Plist 文件

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
更多精彩
    赞助商链接

    热点阅读
      焦点图片
        最新推荐
          精彩阅读