
这是代码:
#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时,我没有收到任何错误,但程序停止,它将我带到调试器.当我在调试器中按继续时,我得到:
Program received signal: “EXC_BAD_ACCESS”.
但这可能并不重要.
PS:我是mac编程的新手,我不知道还有什么可以尝试,因为我没有收到错误消息告诉我出了什么问题.
该文件的路径是:
/Users/andre/documents/Contacts.pList
我之前尝试过这个(结果相同),但我读到你只能写入文件夹:
/Users/andre/Desktop/NN/NStableVIEw/build/DeBUG/NStableVIEw.app/Contents/Resources/Contacts.pList
有没有人有想法甚至解释为什么会这样?
解决方法 您正在使用stringByAppendingPathComponent:方法设置filePath.该方法返回一个自动释放的对象. (自动释放的对象在(自动)释放后使用,这可能导致错误的访问错误.)我想改变
[rootPath stringByAppendingPathComponent:@"Contacts.pList"];
成
[[rootPath stringByAppendingPathComponent:@"Contacts.pList"] retain];
将解决你的麻烦.
总结以上是内存溢出为你收集整理的objective-c – 检查文件是否存在时出错全部内容,希望文章能够帮你解决objective-c – 检查文件是否存在时出错所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)