我遇到了以下问题: 我正在尝试使用以下代码创建一个文件:
NSString *homeDirectory = NSHomeDirectory();
NSString *documentDirectory = [homeDirectory stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"Test.txt"];
NSFileHandle *savedFile = nil;
savedFile = [[NSFileHandle FileHandleForWritingAtPath:filePath]retain];
NSString *data = @"testmessage";
[savedFile writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[savedFile synchronizeFile];
[savedFile closeFile];
我的问题如下。在MAC上运行它不会给我任何错误。但文件本身似乎是不存在的,或者至少是不可定位的。 我需要找到并访问文件,我必须能够使用移动iOS设备将其导出到我的Mac
提前感谢:)
h4wkeye
答案 0 :(得分:0)
您需要检查savedFile是否为零(请参阅此处the docs。)
NSString *homeDirectory = NSHomeDirectory();
NSString *documentDirectory = [homeDirectory stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"Test.txt"];
NSFileHandle *savedFile = nil;
savedFile = [[NSFileHandle fileHandleForWritingAtPath:filePath]retain];
if (nil == savedFile) {
NSLog(@"Could not create saved file at %@", filePath);
}
NSString *data = @"testmessage";
[savedFile writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[savedFile synchronizeFile];
[savedFile closeFile];