0

我需要向我的 plist 写入一个字符串,但是找不到 plist,为什么?

在.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]];
    }
4

2 回答 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");
 }

确保您的目录包含 plist 文件。

于 2013-08-14T14:04:34.877 回答
1

// 改为这样做:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *plistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Сamera.plist"];
于 2013-08-14T14:01:27.797 回答