Cocoa Touch读写Plist项目开发代码是本文要介绍的内容,内容不多,本文基于代码实现Cocoa Touch读写Plist,不多说,直接来看代码。
- - (void) getTopData {
- NSString *documentDirectory = [self getDocumentDirectory];
- NSString *fileName = @"top2.plist";
- NSString *finalPath = [documentDirectory stringByAppendingPathComponent: fileName];
- NSMutableDictionary *topRoot = [[NSMutableDictionary alloc] initWithContentsOfFile: finalPath];
- NSLog(@"%d",[topRoot count]);
- }
- - (void) setTopData {
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *documentDirectory = [self getDocumentDirectory];
- NSString *fileName = @"top2.plist";
- NSString *finalPath = [documentDirectory stringByAppendingPathComponent: fileName];
- if (![[NSFileManager defaultManager] fileExistsAtPath: finalPath]) {
- [fileManager changeCurrentDirectoryPath: [documentDirectory stringByExpandingTildeInPath]];
- NSString *topPath = [[NSBundle mainBundle] pathForResource: @"top" ofType: @"plist"];
- NSData *topRoot = [NSDictionary dictionaryWithContentsOfFile: topPath];
- [fileManager createFileAtPath:fileName contents: topRoot attributes:nil];
- }else {
- NSLog(@"file is here!");
- NSData *topRoot = [[NSMutableDictionary alloc] initWithContentsOfFile: finalPath];
- [topRoot setValue: @"oo" forKey: [NSString stringWithFormat: @"key%d",arc4random()%1000]];
- [topRoot writeToFile: finalPath atomically: NO];
- [fileManager createFileAtPath:fileName contents: topRoot attributes:nil];
- [topRoot release];
- }
- }
- - (NSString *)getDocumentDirectory {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
- return [paths objectAtIndex: 0];
- }
小结:详解Cocoa Touch读写Plist项目开发代码的内容介绍完了,希望通过本文的学习能对你有所帮助!