简体   繁体   English

无法创建要写入的文件-iOS

[英]cannot create a file for writing - iOS

So I have encoded a bunch of objects for writing, but I'm having trouble with the file management. 因此,我已经编码了许多用于编写的对象,但是我在文件管理方面遇到了麻烦。 I can't seem to open the existing file, or create a new file. 我似乎无法打开现有文件或创建新文件。

I've tried creating an empty file of the same name, or just creating a new file. 我尝试创建一个同名的空文件,或者只是创建一个新文件。 It doesn't seem to respond (or do anything at all for that matter). 它似乎没有响应(或对此完全不做任何事情)。

edit So I've followed some of the advice here, and now I'm getting a bad access crash. 编辑因此,我在这里遵循了一些建议,现在我遇到了严重的访问崩溃问题。

Here's where I get the path: 这是我得到路径的地方:

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    filePath = [documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"];

And here's the modified method. 这是修改后的方法。

    -(void) writeFile
{
    NSData *encodedObject;
    encodedObject = [NSKeyedArchiver archivedDataWithRootObject: bucketItems];
    NSFileHandle *file;
    file = [NSFileHandle fileHandleForReadingAtPath:filePath];

    if(file == nil)
    {
        NSLog(@"Failed to open a file handle for writing. Attempting to create a file.");
        [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
        file = [NSFileHandle fileHandleForReadingAtPath:filePath];
        if(file == nil)
        {
            NSLog(@"Unable to create new file.");
        }
    }

    [file writeData: encodedObject];
    [file closeFile];
}

You don't have permissions to write to root, you can only access your sandbox, and you are creating a read-only handle. 您没有写根目录的权限,只能访问沙箱, 并且正在创建只读句柄。 Try this way: 尝试这种方式:

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];   
NSString *filePath = [docDir stringByAppendingPathComponent:@"bucketListData.dat"];
NSFileHandle *file = [NSFileHandle fileHandleForWritingAtPath:filePath];

if (!file) {
    NSLog(@"Attempting to create the file");
    if (![[NSFileManager defaultManager] createFileAtPath:filePath
                                                 contents:nil
                                               attributes:nil]) {
        NSLog(@"Failed to create file");
    }
    else {
        file = [NSFileHandle fileHandleForWritingAtPath:filePath];
    }
}

NSLog(@"File handle: %@", file);

You are trying to open a file at the root of the filesystem, but you don't have access to that on iOS. 您正在尝试在文件系统的根目录下打开文件,但无法在iOS上访问该文件。 Create the file in the documents directory instead: 而是在documents目录中创建文件:

NSData* encodedObject = [NSKeyedArchiver archivedDataWithRootObject: bucketItems];

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString* path = [basePath stringByAppendingPathComponent:@"bucketListData.dat"];
NSFileHandle* file = [NSFileHandle fileHandleForReadingAtPath:path];

You need to write the file at the documents directory. 您需要在文件目录中写入文件。 And then use stringByAppendingPathComponent tu create the full path. 然后使用stringByAppendingPathComponent tu创建完整路径。 This should work, I hope this helps... 这应该可以,希望对您有帮助...

-(void) writeFile
{
    NSData *encodedObject;
    encodedObject = [NSKeyedArchiver archivedDataWithRootObject: bucketItems];
    NSFileHandle *file;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory;

    if(paths && [paths count]>0){
        documentsDirectory=[paths objectAtIndex:0];

        file = [NSFileHandle fileHandleForReadingAtPath:[documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"]];

        if(file == nil)
        {
            NSLog(@"Failed to open a file handle for writing. Attempting to create a file.");
            [[NSFileManager defaultManager] createFileAtPath:[documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"] contents:nil attributes:nil];
            file = [NSFileHandle fileHandleForReadingAtPath:[documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"]];
            if(file == nil)
            {
                NSLog(@"Unable to create new file.");
            }
       }

       [file writeData: encodedObject];
       [file closeFile];
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM