background image

20                      break; 
21               name = player.playerName;// This "Player1" denotes the player name could also 
be the computer name
22               innerDict = [self getAllNodeInfoToDictionary:player];
23               [rootObj setObject:innerDict forKey:name]; // This "Player1" denotes the person 
who start this game
24         }
25         player = nil;
26                NSData  *plistData  =  [NSPropertyListSerialization  dataFromPropertyList:
(id)rootObj format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];

    红色部分可以忽略,只是给 rootObj 添加一点内容。这个 plistData 为创建好的 plist 文件,
用其

writeToFile 方法就可以写成文件。下面是代码:

27 /*得到移动设备上的文件存放位置*/
28         NSString *documentsPath = [self getDocumentsDirectory]; 
29  

 

 

 

 

 

 

 NSString 

*savePath 

[documentsPath 

stringByAppendingPathComponent:@"save.plist"];
30     
31         /*存文件*/
32         if (plistData) {
33                 [plistData writeToFile:savePath atomically:YES];
34          }
35          else {
36                 NSLog(errorDesc);
37                 [errorDesc release];
38         }
39
40         - (NSString *)getDocumentsDirectory {  
41  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 NSArray 

*paths 

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
42                 return [paths objectAtIndex:0];  
43         }

4 读取 plist 文件并转化为 NSDictionary

44    NSString *documentsPath = [self getDocumentsDirectory];
45  

 

 

 

 

 

 

 NSString 

*fullPath 

[documentsPath 

stringByAppendingPathComponent:@"save.plist"];
46                NSMutableDictionary*  plistDict  =  [[NSMutableDictionary  alloc] 
initWithContentsOfFile:fullPath];

5 读取一般性文档文件

47   NSString *tmp;
48         NSArray *lines; /*将文件转化为一行一行的*/
49         lines = [[NSString    stringWithContentsOfFile:@"testFileReadLines.txt"]