詳解Cocoa Touch讀寫Plist項目開發(fā)代碼
作者:佚名
本文介紹的是Cocoa Touch讀寫Plist項目開發(fā)代碼,主要是基于代碼來實現(xiàn)Cocoa Touch讀寫Plist的內容,來看詳細代碼。
Cocoa Touch讀寫Plist項目開發(fā)代碼是本文要介紹的內容,內容不多,本文基于代碼實現(xiàn)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項目開發(fā)代碼的內容介紹完了,希望通過本文的學習能對你有所幫助!
責任編輯:zhaolei
來源:
互聯(lián)網(wǎng)