Xcode不会创建文件

时间:2014-10-02 13:59:27

标签: iphone xcode ipad nsfilemanager filehandle

我正在编写一个将文本保存到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

我在控制台中收到“info saved”提示,但是当我转到“iPhone Simulator”文件夹并检查没有文件时。

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

提前致谢!

编辑:

现在我创建了一个文件并添加了一行文本,但是如果我尝试添加一行文本,则整个文件将替换为带有一行文本的新文件。

#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中初始化什么?

再次感谢。

1 个答案:

答案 0 :(得分:1)

将此添加到“protboViewController.h”

@property (nonatomic, strong) NSString* mailLista;

将此添加到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);

这就是我之前在自己的应用程序中执行此操作的方式