检查文件是否存在时出错 [英] Error while checking if a file exists

查看:108
本文介绍了检查文件是否存在时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用writeToFile写一个plist文件,在我写之前我检查该文件是否存在。

I'm trying to write to a plist file using writeToFile, before I write I check whether the file exists.

这是代码:

#import "WindowController.h"

@implementation WindowController

@synthesize contacts;

NSString *filePath;
NSFileManager *fileManager;

- (IBAction)addContactAction:(id)sender {

    NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:
                         [txtFirstName stringValue], @"firstName",
                         [txtLastName stringValue], @"lastName",
                         [txtPhoneNumber stringValue], @"phoneNumber",
                         nil];

    [arrayContacts addObject:dict];

    [self updateFile];
}

- (void)awakeFromNib {
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    filePath    = [rootPath stringByAppendingPathComponent:@"Contacts.plist"];
    fileManager = [NSFileManager defaultManager];

    contacts = [[NSMutableArray alloc] init];

    if ([fileManager fileExistsAtPath:filePath]) {

        NSMutableArray *contactsFile = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
        for (id contact in contactsFile) {
            [arrayContacts addObject:contact];
        }
    }
}

- (void) updateFile {
    if ( ![fileManager fileExistsAtPath:filePath] || [fileManager isWritableFileAtPath:filePath]) {
        [[arrayContacts arrangedObjects] writeToFile:filePath atomically:YES];
    }
}

@end

addContactAction执行我没有得到任何错误,但程序停止,它带给我的调试器。当我在调试器中按继续时,我得到:

When the addContactAction is executed I don't get any error but the program halts and it brings me to the debugger. When I press continue in the debugger I get:

Program received signal:  "EXC_BAD_ACCESS".

但这可能不重要。

PS:我刚刚接触到mac编程,我不知道还有什么可以尝试,因为我没有得到一个错误信息告诉我出了什么问题。

PS: I'm new to mac programming and I don't know what else to try since I don't get an error message that tells me what's going wrong.

文件路径为:


/Users/andre/Documents/Contacts.plist

/Users/andre/Documents/Contacts.plist

我之前尝试过(结果相同),但我读到你只能写入文档文件夹:

I earlier tried this(with the same result), but I read that you can only write to the documents folder:


/Users/andre/Desktop/NN/NSTableView/build/Debug/NSTableView.app/Contents/Resources/Contacts.plist

/Users/andre/Desktop/NN/NSTableView/build/Debug/NSTableView.app/Contents/Resources/Contacts.plist


推荐答案

您正在设置filePath stringByAppendingPathComponent:方法。该方法返回一个自动释放的对象。 (自动释放对象在(自动释放)对象被使用后可能会导致错误的访问错误。)

You are setting filePath with the stringByAppendingPathComponent: method. That method returns an autoreleased object. (Autoreleased object is used after it has been (automatically) released, which could cause the bad access error.)

我想更改

[rootPath stringByAppendingPathComponent:@"Contacts.plist"];

[[rootPath stringByAppendingPathComponent:@"Contacts.plist"] retain];

会解决您的烦恼。

这篇关于检查文件是否存在时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆