无法通过 NSOutputStream 将数据保存在文档目录的给定路径中

Couldn't save data in given path of document directory by NSOutputStream

如何通过 NSOutputStream 将下载的 content/data 保存在文档目录的给定路径中。如果我用 DD 附加文件名,那么它可以工作(ex- DD/contentPath.zip),但是如果我附加一个像 DD/hello/contentPath.zip 这样的路径,那么它就不起作用。为什么 ?我试过如下 -

- (void) downloadCarContents:(NSString *)url forContent:(NSString *)contentPath {

    //NSString *destinationPath = [self.documentDirectory getDownloadContentPath:contentPath];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    AFHTTPRequestOperation *op = [manager GET:url
                                   parameters:nil
                                      success:^(AFHTTPRequestOperation *operation, id responseObject) {


                                      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

                                          //NSLog(@"Error : %@", error.localizedDescription);
                                      }];

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *dest = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"hello/%@.zip", contentPath]];// It doesn't work
    //NSString *dest = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip", contentPath]];// It works

    op.outputStream = [NSOutputStream outputStreamToFileAtPath:dest append:NO];

    [op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        float percentage = (float) (totalBytesRead * 100) / totalBytesExpectedToRead;
        [self.delegate downloadProgression:percentage];
    }];
}

API 不会为您创建文件夹。在您尝试使用它之前,您的代码需要确保它存在。 NSFileManager 可以帮助 createDirectoryAtPath:withIntermediateDirectories:attributes:error: