iphone - 从 .txt 文件中读取 - Objective C

标签 iphone objective-c ios xcode ipad

<分区>

Possible Duplicate:
Read Text file Programmatically using Objective-C

我正在使用以下代码行::

- (void)sync:(NSString *)savedName{

NSLog(@"saved name is this :: %@", savedName);

NSString *savedDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *saveFilePath = [savedDir stringByAppendingPathComponent:savedName];
saveFilePath = [saveFilePath stringByAppendingPathComponent:@"edit.txt"];

NSString *saveString = [NSString stringWithContentsOfFile:saveFilePath encoding:NSUTF8StringEncoding error:nil]; 

NSLog(@"saveString is :: %@ savedName : %@ savefilepath : %@", saveString, savedName, saveFilePath);
}

edit.txt 的内容是:

= WWWW =
[[Category:ssss]]

我想做的是从文件 edit.txt 中读取并将其内容存储在一个字符串中。但是,在 NSLogging 上,对于 saveString,我得到了 null

我在 gdb 中得到以下输出::

splitView[838:f803] saveString is :: (null) savedName : xxxx savefilepath : /Users/xxxx/Library/Application Support/iPhone Simulator/5.1/Applications/D4B3A4CF-E7D0-4D25-B3D3A170A329/Documents/xxxx/edit.t‌​xt`

谁能帮我解决这个错误??我想不通。感谢和问候。

最佳答案

好的,那么我用来从 .txt 文件中读取数据的内容如下

NSString *path;
path = [[NSBundle mainBundle] pathForResource: YOUR_TEXT_FILE_NAME ofType: @"txt"];

现在是一个读取文件的函数,它返回 NSString 作为返回对象。 我对这个函数的调用如下

NSString *data = [self readFile: path]];

现在的功能

-(NSString *)readFile:(NSString *)fileName

{
    NSLog(@"readFile");

    NSString *appFile = fileName;    
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:appFile])        
    {
        NSError *error= NULL;
        NSString *resultData = [NSString stringWithContentsOfFile: appFile encoding: NSUTF8StringEncoding error: &error]; 
        if (error == NULL)            
            return resultData;
    }
    return NULL;
}

希望对你有帮助

快乐编码:)

关于iphone - 从 .txt 文件中读取 - Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11306689/

上一篇:objective-c - iOS 无法识别的选择器发送到实例 0x45e8

下一篇:ios - 容器 View Controller addSubview 异常

相关文章:

ios - 为什么我的 JSON 数组不显示在 UITableView 对象中?

ios - 如何使用 Swift 将带有圆角半径和阴影的 UIView .xib 加载到主视图 Controller 中

iphone - 在 iPhone 上设置表格 View 单元格的背景颜色

iphone - 状态栏未按预期在 iPad 上呈现

iPhone如何验证字符串只是Objective-c中的图像名称或图像url?

Iphone 应用程序存档 - 没有这样的文件或目录

ios - 为什么单元格标签在 UITableView 中返回相同的值?

iphone - 这个计划是为了防止iPhone应用程序客户端欺骗声音吗?

iphone - 使用 IB 在 UIButton 上居中多行文本

objective-c - 多次播放后 AVPlayer 崩溃-