简体   繁体   English

使用位于文档目录中的plist

[英]Using a plist located in documents directory

Im copying the plist array of dictionaries to documents directory on the device like this: 我将字典的plist数组复制到设备上的documents目录中,如下所示:

AppDelegate.m: AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self performSelector:@selector(copyPlist)];
    return YES;
}

- (void)copyPlist {

    NSError *error;

    NSFileManager *fileManager=[NSFileManager defaultManager];
    NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];
    NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"Wine.plist"];

    if ([fileManager fileExistsAtPath:destinationPath]){
        NSLog(@"database localtion %@",destinationPath);
        return;
    }

    NSString *sourcePath= [[NSBundle mainBundle] pathForResource:@"Wine" ofType:@"plist"];
    NSLog(@"source path %@",sourcePath);
    [fileManager copyItemAtPath:sourcePath toPath:destinationPath error:&error];
    }

Log for database location: 记录数据库位置:

/var/mobile/Applications/832E16F4-A204-457E-BFF0-6AEA27915C25/Documents/Wine.plist

Then, I'm trying to access the plist and fill sortedWines array with the dictionaries to populate a TableView: 然后,我尝试访问plist并用字典填充sortedWines数组以填充TableView:

TableViewController.h TableViewController.h

#import <UIKit/UIKit.h>

@interface WinesViewController : UITableViewController <UIActionSheetDelegate> {
     NSMutableArray *sortedWines;
}

@end

TableViewController.m TableViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Wine.plist"];
    NSLog(@"plist path %@", path);

    sortedWines = [[NSMutableArray alloc] initWithContentsOfFile:path];
    NSLog(@"objects %@", sortedWines);


    NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Popularity" ascending:YES];
    [sortedWines sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];

    [super viewDidLoad];
}

The log for plist path: plist路径的日志:

/var/mobile/Applications/832E16F4-A204-457E-BFF0-6AEA27915C25/Documents/Wine.plist

and the log for objects: 和对象的日志:

(null) (空值)

How to fill the sortedWines array with objects now? 现在如何用对象填充sortedWines数组?

So the solution is that your original plist file in your bundle is not structured as an array of dictionaries. 因此,解决方案是您捆绑包中的原始plist文件未构造为字典数组。

To settle this assertion add a few more lines to your copyPlist method: 要解决此断言,请向您的copyPlist方法添加几行:

NSString *sourcePath= [[NSBundle mainBundle] pathForResource:@"Wine" ofType:@"plist"];
NSLog(@"source path %@",sourcePath);
NSMutableArray *sortedWines = [[NSMutableArray alloc] initWithContentsOfFile:sourcePath];
NSLog(@"objects %@", sortedWines); // bet this is nil too

EDIT 编辑

So once you know you have the file above then you need to view/test 因此,一旦您知道上面有文件,就需要查看/测试

[fileManager copyItemAtPath:sourcePath toPath:destinationPath error:&error]; [fileManager copyItemAtPath:sourcePath toPath:destinationPath error:&error];

The problem here is you do not check the return code but blindly assume success. 这里的问题是您不检查返回码而是盲目地假设成功。 So look at return code and log error in it fails. 因此,请查看返回码,并记录失败的错误。 Once you know that is working then also add another line just after the copy to load the file into an array as your viwDidLoad does (copy the code). 一旦知道它正在工作,则还可以在复制后添加另一行,以将文件加载到数组中,就像viwDidLoad一样(复制代码)。

If that works but file is missing at viewDidLoad then the only possible is you or the system is deleting all files in the documents directory (I have heard if your app uses iCloud the rules for this folder change. 如果可行,但viewDidLoad缺少文件,则唯一的可能是您或系统正在删除documents目录中的所有文件(我听说如果您的应用程序使用iCloud,则此文件夹的规则将更改。

PS: why are you doing this: PS:您为什么这样做:

[self performSelector:@selector(copyPlist)];

Instead of: 代替:

[self copyPlist];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM