【问题标题】:Xcode does not create fileXcode 不创建文件
【发布时间】:2014-10-02 13:59:26
【问题描述】:

我正在编写一个将文本保存到 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 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 中初始化什么?

再次感谢。

【问题讨论】:

    标签: iphone xcode ipad nsfilemanager filehandle


    【解决方案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);
    

    这就是我之前在我自己的一个应用中所做的方式

    【讨论】:

    • 不,这不是有效的更改。 OP 正在尝试附加到现有文件(如果它已经存在)。您的更改将始终覆盖任何现有文件。
    • @rmaddy 你说得对,我没有注意到他的意图。没有我的改变,它至少打印了正确的路径吗?
    • @KevinGriesbach 我已经做到了,现在它会创建一个文件,但它不会添加新行,而是覆盖所有内容。我将编辑我的第一篇文章,如果您能看一下,我将不胜感激。
    • 我刚刚更新了我的答案,试一试 - 虽然它看起来与您的第一次尝试非常相似。
    猜你喜欢
    • 1970-01-01
    • 2011-07-18
    • 2011-07-13
    • 1970-01-01
    • 2012-12-14
    • 2017-11-19
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多