简体   繁体   中英

Using a plist located in documents directory

Im copying the plist array of dictionaries to documents directory on the device like this:

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:

TableViewController.h

#import <UIKit/UIKit.h>

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

@end

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:

/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?

So the solution is that your original plist file in your bundle is not structured as an array of dictionaries.

To settle this assertion add a few more lines to your copyPlist method:

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];

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).

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.

PS: why are you doing this:

[self performSelector:@selector(copyPlist)];

Instead of:

[self copyPlist];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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