【问题标题】:Not able to write nsdata in a folder无法在文件夹中写入 nsdata
【发布时间】:2013-02-06 19:01:00
【问题描述】:

以下是代码:

NSData * imageData = [NSData dataWithContentsOfURL:imageURL];                

 [imageData writeToFile:savedImagePath options:NSDataWritingAtomic error:&error];
                            if(error != nil)
 NSLog(@"write error %@", error);   

错误:

 write error Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x8b7b850 {NSUnderlyingError=0x8b7d7c0 "The operation couldn’t be completed. No such file or directory", NSFilePath=/Users/alfa-1/Library/Application Support/iPhone Simulator/6.0/Applications/C24B228A-599E-4249-97A7-17775E8A546B/Library/Caches/ChannelListImages/http:/direct.domain.com/files/icons/1-all-cb.jpg, NSUserStringVariant=Folder}

【问题讨论】:

  • 也许你必须创建中间文件夹...
  • 在 NSLog 中显示你的 savedImagePath。路径可能无效
  • 什么是中间文件夹?
  • 如果路径是 /folderA/folderB/folderC。 folderA 和 folderB 将是中间文件夹。如果要保存在文件夹C文件夹A中,B和C必须存在才能保存。
  • @MohammedEbrahim 什么是 savedImagePath?

标签: ios nsdata writetofile


【解决方案1】:

在将数据写入文件之前。您需要创建文件夹。请按照以下步骤操作

#define APPLICATION_DATA_DIRECTORY @"Application Data"
+ (NSString *)cachesDirectoryPath
// Returns the path to the caches directory.  This is a class method because it's
// used by +applicationStartup.
{
    NSString *      result;
    NSArray *       paths;

    result = nil;
    paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    if ( (paths != nil) && ([paths count] != 0) ) {
        assert([[paths objectAtIndex:0] isKindOfClass:[NSString class]]);
        result = [paths objectAtIndex:0];
    }
    result = [result stringByAppendingPathComponent:APPLICATION_DATA_DIRECTORY];
    if (![[NSFileManager defaultManager] fileExistsAtPath:result]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:result withIntermediateDirectories:YES attributes:nil error:NULL];
    }

    return result;
}

之后:#define kPhotosDirectoryName @"ApplicationDocuments"

    NSMutableString *savePath = [NSMutableString string];
    [savePath appendFormat:@"%@/%@",[YOURClass cachesDirectoryPath],kPhotosDirectoryName];

    [savePath appendFormat:@"/%@",YOUR_FILE_NAME];

    if (![[NSFileManager defaultManager] fileExistsAtPath:savePath]) {
        //write the data to file here
    }

【讨论】:

    【解决方案2】:

    试试这个示例。它对我有用。

    在 .h 文件中声明 -(NSString*) 数据文件路径

    -(NSString *) datafilepath{
    NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents=[path objectAtIndex:0];
    return [documents stringByAppendingFormat:@"/sample.plist"];
    }
    

    任何你想执行写入功能的地方

    NSData * imageData = [NSData dataWithContentsOfURL:imageURL];                
    [imageData writeToFile:[self datafilepath]atomically:YES];
     if(error != nil)
     NSLog(@"write error %@", error);   
    

    希望对你有帮助!!!

    【讨论】:

    • 应该是[documents stringByAppendingPathComponent:@"sample.plist"];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 2018-10-11
    相关资源
    最近更新 更多