详解Cocoa Touch读写Plist项目开发代码


  本文标签:Cocoa Touch Plist

  Cocoa Touch读写Plist项目开发代码是本文要介绍的内容,内容不多,本文基于代码实现Cocoa Touch读写Plist,不多说,直接来看代码  。

  1. - (void) getTopData {  
  2.  
  3.    NSString *documentDirectory = [self getDocumentDirectory];  
  4.    NSString *fileName = @"top2.plist";  
  5.    NSString *finalPath = [documentDirectory stringByAppendingPathComponent: fileName];  
  6.  
  7.    NSMutableDictionary *topRoot = [[NSMutableDictionary alloc] initWithContentsOfFile: finalPath];  
  8.    NSLog(@"%d",[topRoot count]);  
  9. }  
  10.  
  11. - (void) setTopData {  
  12.  
  13.    NSFileManager *fileManager = [NSFileManager defaultManager];  
  14.    NSString *documentDirectory = [self getDocumentDirectory];  
  15.    NSString *fileName = @"top2.plist";  
  16.    NSString *finalPath = [documentDirectory stringByAppendingPathComponent: fileName];  
  17.  
  18.    if (![[NSFileManager defaultManager] fileExistsAtPath: finalPath]) {  
  19.      [fileManager changeCurrentDirectoryPath: [documentDirectory stringByExpandingTildeInPath]];  
  20.  
  21.      NSString *topPath = [[NSBundle mainBundle] pathForResource: @"top" ofType: @"plist"];  
  22.      NSData *topRoot = [NSDictionary dictionaryWithContentsOfFile: topPath];  
  23.  
  24.      [fileManager createFileAtPath:fileName contents: topRoot attributes:nil];  
  25.  
  26.    }else {  
  27.      NSLog(@"file is here!");  
  28.      NSData *topRoot = [[NSMutableDictionary alloc] initWithContentsOfFile: finalPath];  
  29.      [topRoot setValue: @"oo" forKey: [NSString stringWithFormat: @"key%d",arc4random()%1000]];  
  30.      [topRoot writeToFile: finalPath atomically: NO];  
  31.      [fileManager createFileAtPath:fileName contents: topRoot attributes:nil];  
  32.      [topRoot release];  
  33.    }  
  34. }  
  35.  
  36. - (NSString *)getDocumentDirectory {  
  37.    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);  
  38.    return [paths objectAtIndex: 0];  

  小结:详解Cocoa Touch读写Plist项目开发代码的内容介绍完了,希望通过本文的学习能对你有所帮助!