我需要给我的plist写一个字符串,但是找不到plist,为什么?
in .h
NSMutableDictionary *cameras;
.m文件
#define DOCUMENTS [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
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]];
}
答案 0 :(得分: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");
}
确保您的目录包含plist文件。
答案 1 :(得分:1)
//改为:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Сamera.plist"];