如何从文本字段读取用户输入文本并将其写入 Xcode 中的文本文件?

发布于 2024-12-13 18:24:21 字数 218 浏览 0 评论 0原文

有没有办法将从文本字段读取的 string 写入 .txt 文件?

NSString *input = [textfield text];

NSString *path = @"myText.txt";

[input writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];

Is there a way to write a string , which is read from text field, to a .txt file?

NSString *input = [textfield text];

NSString *path = @"myText.txt";

[input writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百善笑为先 2024-12-20 18:24:21

是的,最终就是这样;但是为了从开始到结束,您还需要执行许多其他步骤(您需要将文本字段连接到 IBOutlet 或以其他方式可访问;myText.txt 写入的路径需要是可写的,因此您可能需要有一个比文件名更长、更精确的路径;您可能还需要发送一个可以设置的实际错误参数,以便您可以查看 writeToFile 调用第一次失败时返回的错误你运行这个几次 代码)。

Yes, ultimately that is the way; but there's a number of other steps you do in order to get from start to finish (you need to have textfield connected to an IBOutlet or in some other way accessible; the path where myText.txt is writing to needs to be writable and therefore you'd probably need to have a longer, more precise path than just the filename; you'd probably need to also send in an actual error parameter that could be set so you could look at the errors being returned when the writeToFile call fails the first few times you run this code).

客…行舟 2024-12-20 18:24:21

您需要定义一个路径,基本上是保存文件的位置。
该代码将找到 Documents 文件夹

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

这是我用来读取和写入文件的代码。

-(void)writeFileToDisk:(id)data 
{ 
    NSArray *path = NSSearchPathForDirectoriesInDomains( 
            NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentDirPath = [path objectAtIndex:0]; 
    NSString *fileName = @"MyFileName.txt"; 

    NSString *fileAndPath = [documentDirPath stringByAppendingPathComponent:fileName]; 

    [data writeToFile:fileAndPath atomically:YES]; 
} 

-(void)readFileFromDisk 
{ 
    NSArray *path = NSSearchPathForDirectoriesInDomains( 
            NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentDirPath = [path objectAtIndex:0]; 
    NSString *fileName = @"MyFileName.txt"; 

    NSString *fileAndPath = [documentDirPath stringByAppendingPathComponent:fileName]; 

    NSArray *array = [[NSArray alloc] initWithContentsOfFile:fileAndPath]; 
    NSLog(@"%@",array); 
    [array release]; 
} 

You will need to define a path, basically where to save the file.
This code will find the Documents folder

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

Here is the code I use to read and write files.

-(void)writeFileToDisk:(id)data 
{ 
    NSArray *path = NSSearchPathForDirectoriesInDomains( 
            NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentDirPath = [path objectAtIndex:0]; 
    NSString *fileName = @"MyFileName.txt"; 

    NSString *fileAndPath = [documentDirPath stringByAppendingPathComponent:fileName]; 

    [data writeToFile:fileAndPath atomically:YES]; 
} 

-(void)readFileFromDisk 
{ 
    NSArray *path = NSSearchPathForDirectoriesInDomains( 
            NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentDirPath = [path objectAtIndex:0]; 
    NSString *fileName = @"MyFileName.txt"; 

    NSString *fileAndPath = [documentDirPath stringByAppendingPathComponent:fileName]; 

    NSArray *array = [[NSArray alloc] initWithContentsOfFile:fileAndPath]; 
    NSLog(@"%@",array); 
    [array release]; 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文