如何将一个plist从包移动到iOS的文档文件夹?

时间:2023-02-06 12:39:56

So all I want to know is how to move a plist from the bundle to the documents folder the first time an application runs, because I need it. Please I can't find out how to do it, so help me.

所以我只想知道如何在应用程序第一次运行时将plist从bundle移动到documents文件夹,因为我需要它。求你了,我找不到怎么做,所以帮帮我。

3 个解决方案

#1


4  

If your plist name is "friends"

如果你的plist名称是“朋友”

just use below code ( it will find whether the file exists or not and copies)

只需使用下面的代码(它将查找文件是否存在并复制)

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

NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];

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

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

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

that will copy plist from resources to documents directory

这将把plist从资源复制到documents目录

#2


2  

This function should do the trick.

这个函数应该是这样的。

- (void)copyPlist {
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"myplist" ofType:@"plist"];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    [[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil];
}

You don't need to use the NSError argument in the [[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil]; line, but it may be useful for debugging.

您不需要在[[NSFileManager defaultManager] copyItemAtPath中使用NSError参数:plistPath toPath:documentsDirectory error:nil];行,但是它可能对调试有用。

#3


0  

Just a heads up... this came from http://samsoff.es/posts/iphone-plist-tutorial "Using JSON is highly preferred over Plists these days since JSON parsers have come a long way in speed. They are actually faster than binary plists now (which is nuts). Anyway, please, please, please don't use plists for your API. Generating them is slow and unreliable. You should use JSON. Period."

只是一个头…这来自http://samsoff.es/posts/iphone-plist-tutorial,“使用JSON比Plists更受欢迎,因为JSON解析器在速度上已经取得了长足的进步。”它们实际上比现在的二进制编码要快(简直是疯了)。不管怎样,求你了,请不要用钳子做API。生成它们很慢而且不可靠。您应该使用JSON。期。”

#1


4  

If your plist name is "friends"

如果你的plist名称是“朋友”

just use below code ( it will find whether the file exists or not and copies)

只需使用下面的代码(它将查找文件是否存在并复制)

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

NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];

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

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

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

that will copy plist from resources to documents directory

这将把plist从资源复制到documents目录

#2


2  

This function should do the trick.

这个函数应该是这样的。

- (void)copyPlist {
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"myplist" ofType:@"plist"];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    [[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil];
}

You don't need to use the NSError argument in the [[NSFileManager defaultManager] copyItemAtPath:plistPath toPath:documentsDirectory error:nil]; line, but it may be useful for debugging.

您不需要在[[NSFileManager defaultManager] copyItemAtPath中使用NSError参数:plistPath toPath:documentsDirectory error:nil];行,但是它可能对调试有用。

#3


0  

Just a heads up... this came from http://samsoff.es/posts/iphone-plist-tutorial "Using JSON is highly preferred over Plists these days since JSON parsers have come a long way in speed. They are actually faster than binary plists now (which is nuts). Anyway, please, please, please don't use plists for your API. Generating them is slow and unreliable. You should use JSON. Period."

只是一个头…这来自http://samsoff.es/posts/iphone-plist-tutorial,“使用JSON比Plists更受欢迎,因为JSON解析器在速度上已经取得了长足的进步。”它们实际上比现在的二进制编码要快(简直是疯了)。不管怎样,求你了,请不要用钳子做API。生成它们很慢而且不可靠。您应该使用JSON。期。”