开发者

Question concerning File Handling

i got the following problem : i am trying to create a file with the following Code :

NSString *homeDirectory = NSHomeDirectory();
NSString *documentDirectory = [homeDirectory stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"Test.txt"];

NSFileHandle *savedFile = nil;
savedFile = [[NSFileHandle FileHandleForWritingAtPath:filePath]retain];

NSString *d开发者_StackOverflow社区ata = @"testmessage";
[savedFile writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[savedFile synchronizeFile];
[savedFile closeFile];

My Question is the following. Running this on a MAC does not give me any Errors whatsoever. But the File itself seems to be nonexistant or at least not locateable. I need to find and access the File and i have to be able to export it to my Mac using a mobile iOS Device

thanks in advance :)

h4wkeye


You need to check that savedFile is not nil (see the docs here).

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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜