在iPhone上书写和阅读文本文件 [英] Writing and reading text files on the iPhone

查看:158
本文介绍了在iPhone上书写和阅读文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一种做法,我正在尝试编写类似于内置的笔记应用程序的应用程序。

但是我不知道如何保存文件并显示它在一个 UITableView

现在,我有一个 UITextView ,用户可以输入。我还有一个保存按钮。

当用户点击保存按钮时,我想保存它,稍后将它显示在表视图中。

我很迷茫,所以如果你知道任何相关的教程等,将不胜感激。

解决方案

通过现实世界中的评论者,你肯定会想看看Core Data或其他一些数据持久化策略。如果你没有把追求这个作为一个学习经验,这样的事情应该可以解决你的问题:

pre $ - (void) writeStringToFile:(NSString *)aString {

//构建路径,并根据需要创建。
NSString * filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
NSString * fileName = @myTextFile.txt;
NSString * fileAtPath = [filePath stringByAppendingPathComponent:fileName];
$ b $ if(![NSFileManager defaultManager] fileExistsAtPath:fileAtPath]){
[[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil];

$ b $ //主操作...
[[aString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO];

$ b $(NSString *)readStringFromFile {

//建立路径...
NSString * filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask ,YES)objectAtIndex:0];
NSString * fileName = @myTextFile.txt;
NSString * fileAtPath = [filePath stringByAppendingPathComponent:fileName];
$ b //主要行为...
return [[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileAtPath] encoding:NSUTF8StringEncoding] autorelease];
}


As a practice, I am trying to write an app similar to the built-in notes app.
But I cannot figure out how to save the file and display it in a UITableView.
Right now, I have a UITextView that the user can type in. I also have a save button.
When the user taps the save button, I want to save it, and later have it displayed in a table view.
I am very lost so if you know of any relevant tutorials etc. it would be greatly appreciated.

解决方案

As noted by the commenters in the real world, you're definitely going to want to look at Core Data or some other data persistence strategy. If you're dead set on pursuing this as a learning experience, something like this should solve your problem:

- (void)writeStringToFile:(NSString*)aString {

    // Build the path, and create if needed.
    NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* fileName = @"myTextFile.txt";
    NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];

    if (![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) {
        [[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil];
    }

    // The main act...
    [[aString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO];
}

- (NSString*)readStringFromFile {

   // Build the path...
   NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
   NSString* fileName = @"myTextFile.txt";
   NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];

   // The main act...
   return [[[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileAtPath] encoding:NSUTF8StringEncoding] autorelease];
}

这篇关于在iPhone上书写和阅读文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆