转载:

怎么在xcode工程中创建自己的plist文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"]; NSFileManager *fileManager = [NSFileManager defaultManager];



if (![fileManager fileExistsAtPath: path]) 

{

    path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"plist.plist"] ];

}



NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];





NSFileManager *fileManager = [NSFileManager defaultManager];

NSMutableDictionary *data;



if ([fileManager fileExistsAtPath: path]) 

{

            data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

}

else

{

    // If the file doesn’t exist, create an empty dictionary

    data = [[NSMutableDictionary alloc] init];

}



//To insert the data into the plist

int value = 5;

[data setObject:[NSNumber numberWithInt:value] forKey:@"value"];

[data writeToFile: path atomically:YES];

[data release];



//To reterive the data from the plist

NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

int value1;

value1 = [[savedStock objectForKey:@"value"] intValue];

NSLog(@"%i",value1);

[savedStock release];
 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-22
  • 2022-12-23
  • 2021-05-11
  • 2022-12-23
  • 2021-11-03
猜你喜欢
  • 2022-12-23
  • 2021-09-21
  • 2021-04-29
  • 2021-09-05
  • 2022-12-23
  • 2021-11-03
  • 2021-06-23
相关资源
相似解决方案