0

所以我已经对一堆对象进行了编码,但我在文件管理方面遇到了麻烦。我似乎无法打开现有文件或创建新文件。

我试过创建一个同名的空文件,或者只是创建一个新文件。它似乎没有回应(或为此做任何事情)。

编辑所以我在这里遵循了一些建议,现在我遇到了严重的访问崩溃。

这是我得到路径的地方:

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

这是修改后的方法。

    -(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];
}
4

3 回答 3

4

您没有写入 root 的权限,您只能访问您的沙箱,并且您正在创建一个只读句柄。试试这个方法:

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);
于 2013-04-01T21:51:13.843 回答
1

您试图在文件系统的根目录打开一个文件,但在 iOS 上您无权访问该文件。而是在文档目录中创建文件:

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];
于 2013-04-01T21:50:24.243 回答
1

您需要将文件写入文档目录。然后使用 stringByAppendingPathComponent tu 创建完整路径。这应该有效,我希望这会有所帮助......

-(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];
   }
}
于 2013-04-01T21:55:27.883 回答