【问题标题】:iOS: Copying file from bundle on application start-upiOS:在应用程序启动时从包中复制文件
【发布时间】:2011-10-10 05:02:36
【问题描述】:

当用户第一次使用我的应用程序时,它应该从包中复制一个配置文件到某个文件夹中。然后用户可以摆弄这个文件,如果他们把它弄乱了,他们可以简单地按“恢复”,这将删除该文件并从包中再次复制它。

- (void) resetPresets
{
    LOG_( @"Copying tunings file from original..." );

    // copy default tunings -> curr tunings file

    NSString* appSupportDir = [NSFileManager appSupportDir];
    NSString* tuningsPath = [appSupportDir stringByAppendingPathComponent: @"tunings.txt"];

    NSBundle* bundle = [NSBundle mainBundle];
    NSString* origTuningsPath = [bundle pathForResource: @"tuningsOriginal"
                                                 ofType: @"txt" ];


    NSFileManager* fileManager = [NSFileManager defaultManager];
    NSError* error = nil;

    if( [fileManager fileExistsAtPath: tuningsPath] )
    {
        [fileManager removeItemAtPath: tuningsPath
                                error: & error ];
        if( error ) 
            LOG( @"\n ERROR: %@ \n %@ \n", [error userInfo], [error localizedFailureReason] );
    }


    assert( [fileManager fileExistsAtPath: origTuningsPath] );

    [fileManager copyItemAtPath: origTuningsPath
                         toPath: tuningsPath
                          error: & error ];

    if( error ) 
        LOG( @"\n ERROR: %@ \n %@ \n", [error userInfo], [error localizedFailureReason] );


    LOG( @"done!" );


    // load profiles from it
    [self loadProfilesFromFile: tuningsPath ];

    // auto-sets active preset index to 0 & saves prefs
    self.activeThemeIndex = 0;
}

依赖于一个简单的类别:

#import "NSFileManager+addons.h"


@implementation NSFileManager ( NSFileManager_addons )

+ (NSString *) appSupportDir
{

    NSArray* paths = NSSearchPathForDirectoriesInDomains(
                                                         NSApplicationSupportDirectory,
                                                         NSUserDomainMask, 
                                                         YES
                                                         );

    NSString* appSupportDir = [paths objectAtIndex: 0];

    return appSupportDir;
}

@end

这是导致问题的行:

    [fileManager copyItemAtPath: origTuningsPath
                         toPath: tuningsPath
                          error: & error ];

这是控制台输出:

[presets init] Presets -> 首先运行!使用默认设置 预设从原始复制调音文件...错误:{ NSDestinationFilePath = "/var/mobile/Applications/38FC3C65-74AF-4892-B48D-A3508A8CF404/库/应用程序 支持/tunings.txt"; NSFilePath = “/var/mobile/Applications/38FC3C65-74AF-4892-B48D-A3508A8CF404/Fork.app/tuningsOriginal.txt”; NSUserStringVariant = 复制; } 没有这样的文件或目录

为什么抱怨没有这样的文件或目录?显然不应该存在这样的文件。当您将文件复制到新位置时,您不希望文件在那里。

所以我猜它在抱怨目录。但是我已经使用一种相当标准的方法将目录捞出。到底是怎么回事?这不是要使用的正确目录吗?还是我做错了什么?

【问题讨论】:

    标签: ios copy bundle nsfilemanager


    【解决方案1】:

    NSSearchPathForDirectoriesInDomains返回的目录不保证存在;如有必要,您需要自己创建它们(请参阅the documentation)。 NSFileManager 的createDirectoryAtPath:withIntermediateDirectories:attributes:error: 可以提供帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      相关资源
      最近更新 更多