Iphone Xcode不创建文件

Iphone Xcode不创建文件,iphone,xcode,ipad,nsfilemanager,filehandle,Iphone,Xcode,Ipad,Nsfilemanager,Filehandle,我正在编写一个将文本保存到CSV文件的应用程序。但在模拟器中尝试后,我在路径中找不到该文件 代码如下: #import "protboViewController.h" @interface protboViewController () @end @implementation protboViewController @synthesize email; - (IBAction)retractKeyboard:(id)sender{ [self resignFirst

我正在编写一个将文本保存到CSV文件的应用程序。但在模拟器中尝试后,我在路径中找不到该文件

代码如下:

    #import "protboViewController.h"

@interface protboViewController ()

@end

@implementation protboViewController
@synthesize email;

- (IBAction)retractKeyboard:(id)sender{
    [self resignFirstResponder];
}
- (IBAction)saveInfo:(id)sender {
    NSString *resultLine=[NSString stringWithFormat:@"%@\n",
                          self.email.text];

    NSString *docPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];

    NSString *mailLista=[docPath stringByAppendingPathComponent:@"elista.csv"];

                          if  (![[NSFileManager defaultManager] fileExistsAtPath:docPath]) {
                              [[NSFileManager defaultManager] createFileAtPath:mailLista contents:nil attributes:nil];
                          }
                          NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:mailLista];
                          [fileHandle seekToEndOfFile];
                          [fileHandle writeData:[resultLine dataUsingEncoding:NSUTF8StringEncoding]];
                          [fileHandle closeFile];
                            self.email.text=@"";
                          NSLog(@"info saved");
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
我在控制台中得到“信息保存”提示,但当我转到“iPhone模拟器”文件夹并检查没有文件时

另外,如何从设备将此文件导出到计算机

提前谢谢

编辑:

现在我已经创建了一个文件并添加了一行文本,但是如果我尝试添加一行文本,整个文件将被一个新文件替换为一行文本

#import "protboViewController.h"

@interface protboViewController ()

@end

@implementation protboViewController
@synthesize email;


- (IBAction)retractKeyboard:(id)sender{
    [self resignFirstResponder];
}
- (IBAction)saveInfo:(id)sender {
    NSString *resultLine=[NSString stringWithFormat:@"%@\n",
                          self.email.text];

    NSArray *docPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *docDirectory = [docPath objectAtIndex:0];

    NSString *mailLista=[docDirectory stringByAppendingPathComponent:@"elista.csv"];

    NSError *csvError = NULL;

    BOOL written = [resultLine writeToFile:mailLista atomically:YES encoding:NSUTF8StringEncoding error:&csvError];

    if (!written)
        NSLog(@"write failed, error=%@", csvError);
    else
        NSLog(@"Saved! File path =", mailLista);

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
我搜索了一个答案,发现了这样一句话:“既然您没有使用for循环,请确保在头文件中声明outputString。在viewDidLoad中初始化它,然后您就可以附加到它了。”

如何在头文件中声明它,以及在viewDidLoad中初始化什么

再次感谢。

将此添加到您的“protboViewController.h”中

将此添加到您的viewDidLoad:

NSString *docPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
self.mailLista = [docPath stringByAppendingPathComponent:@"elista.csv"];
if  (![[NSFileManager defaultManager] fileExistsAtPath:docPath]) {
    [[NSFileManager defaultManager] createFileAtPath:self.mailLista contents:nil attributes:nil];
}
最后,将您的saveInfo更改为以下内容:

NSString *resultLine=[NSString stringWithFormat:@"%@\n", self.email.text];

NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:self.mailLista];
[fileHandle seekToEndOfFile];
[fileHandle writeData:[resultLine dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
self.email.text=@"";

NSLog(@"Saved! File path =%@", self.mailLista);

我以前在自己的一个应用程序中就是这样做的

不,这不是一个有效的改变。OP正在尝试附加到现有文件(如果该文件已存在)。您所做的更改将始终覆盖任何现有文件。@rmaddy您是对的,我没有注意到他的意图。如果没有我的更改,它是否至少打印了正确的路径?@KevinGriesbach我这样做是为了让它现在创建一个文件,但它没有添加新行,而是覆盖了所有内容。我将编辑我的第一篇文章,如果你能看一看,我将非常感激。我刚刚更新了我的答案,尝试一下——尽管它看起来与你的第一次尝试非常相似。
NSString *resultLine=[NSString stringWithFormat:@"%@\n", self.email.text];

NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:self.mailLista];
[fileHandle seekToEndOfFile];
[fileHandle writeData:[resultLine dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
self.email.text=@"";

NSLog(@"Saved! File path =%@", self.mailLista);