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項目開發代碼的內容介紹完了,希望通過本文的學習能對你有所協助!