读书人

读取封存plist文件

发布时间: 2012-07-25 09:43:06 作者: rapoo

读取保存plist文件

@implementation SettingUtil

+ (NSString *)dataFilePath{
??? NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
??? NSString *documentsDirectory = [paths objectAtIndex:0];
??? return [documentsDirectory stringByAppendingPathComponent:@"config.plist"];
}

+(NSMutableDictionary *)loadFromFile {
??? NSString *error = nil;
??? NSPropertyListFormat format;
??? NSMutableDictionary *dict = nil;
??? NSString *filePath = [self dataFilePath];
??? if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
??? ??? filePath = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"];
??? }
??? NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:filePath];
??? dict = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?? mutabilityOption:NSPropertyListMutableContainersAndLeaves
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?format:&format
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?? errorDescription:&error];
??? return dict;
}

+(BOOL)saveToFile:(NSMutableDictionary *)withData {
??? NSString *error = nil;
??? NSData??? *plistData = [NSPropertyListSerialization dataFromPropertyList:withData format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
??? if(plistData) {
??? ??? return [plistData writeToFile:[self dataFilePath] atomically:YES];
??? } else {
??? ??? return FALSE;
??? }
}

读书人网 >移动开发

热点推荐