简体   繁体   中英

Copy plist to documents directory and read from it

I'm copying the plist to documents directory when didFinishLaunchingWithOptions in AppDelegate like this:

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

- (void)copyPlist {
    NSFileManager *fileManger=[NSFileManager defaultManager];
    NSError *error;
    NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];

    NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"Obj.plist"];

    if ([fileManger fileExistsAtPath:destinationPath]){
        NSLog(@"database localtion %@",destinationPath);
        return;
    }
    NSString *sourcePath=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"Obj.plist"];

    [fileManger copyItemAtPath:sourcePath toPath:destinationPath error:&error];
}

Then I'm trying to display the content of the plist in my views like this:

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

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

LOG RESULTS:

database localtion:

/Users/kdb/Library/Application Support/iPhone Simulator/5.1/Applications/4E165740-01CD-4ED3-8971-FDCCB1751DD1/Documents/Obj.plist

plist path:

the same as database location

objects:

(null)

The content is empty. How to make it work?

The plist is an array with dictionaries.

Have you tried to NSLog the path for the resource in the bundle? Does it look correct? Apart from that, I think you should locate it using this other method:

- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension

So, your method invocation would be:

NSString *sourcePAth = [[NSBundle mainBundle] pathForResource:@"Obj" ofType:@"plist"];

Then, I don't think you can do [[NSMutableArray alloc] initWithContentsOfFile:path]; . Rather, try using a NSDictionary:

NSDictionary *loadedPlist = [[NSDictionary alloc] initWithContentsOfFile:pathString];
//and then, if you need to edit it:
NSMutableDictionary *workingCopy = [loadedPlist mutableCopy];

Just read your edit to the question. plists cannot be considered Arrays, they are always dictionaries and thus must be deserialized using NSDictionary.

I can't tell why it only works the first time without logs. Could you post them? Also, even if it seems to work using NSMutableArray, it would be easier to debug the issue if you adhered to the NSDisctionary style of deserialization.

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