i need to write a string to my plist, but a can't find plist, why?
我需要给我的plist写一个字符串,但是找不到plist,为什么?
in .h
NSMutableDictionary *cameras;
.m file
#define DOCUMENTS [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
in ViewDidLoad
NSString *plistPath = [DOCUMENTS stringByAppendingPathComponent:@"Сamera.plist"];
if([[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
cameras = [NSMutableDictionary dictionaryWithDictionary:[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:nil errorDescription:nil]];
}
2 个解决方案
#1
1
// Do this instead:
//这样做:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Сamera.plist"];
#2
2
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:@"YourPlist.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath]) {
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"YourPlist" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath:docDirPath error:&error];
NSLog(@"plist is copied to Directory");
}
Make sure your directory contains the plist file.
确保您的目录包含plist文件。
#1
1
// Do this instead:
//这样做:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Сamera.plist"];
#2
2
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *docDirPath = [documentsDirectory stringByAppendingPathComponent:@"YourPlist.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: docDirPath]) {
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"YourPlist" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath:docDirPath error:&error];
NSLog(@"plist is copied to Directory");
}
Make sure your directory contains the plist file.
确保您的目录包含plist文件。