gpt4 book ai didi

ios - 在 UITextView 中显示 txt 文件

转载 作者:行者123 更新时间:2023-11-29 04:56:08 25 4
gpt4 key购买 nike

我是 iPhone 开发新手,需要帮助。我正在开发一个包含 UITextView 的 iPhone 应用程序。我的文档中有一个 txt 文件列表。我正在尝试使用 UITextView 来显示文本文件的内容,但我很难让它显示任何东西。我使用这些代码希望 UITextView 显示 txt 文件:

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
self.frlistName2 = [[manager contentsOfDirectoryAtPath:documentsDirectoryPath error:nil]pathsMatchingExtensions:[NSArray arrayWithObject:@"txt"]];
NSString *obj = @".txt";
frlisturls2 = [[NSMutableArray alloc] initWithObjects:nil];
NSString *temp;
NSMutableString *mp3Path = [[NSMutableArray alloc] init];
int j =0;
for(int i =0 ; i< [frlistName2 count]; i++)
{
mp3Path = [NSMutableString stringWithString:documentsDirectoryPath];
[mp3Path appendString:@"/"];

temp = [frlistName2 objectAtIndex:i];
if([temp hasSuffix :obj])
{
[mp3Path appendString: temp];
[frlisturls2 addObject: mp3Path];
j++;
}
[mp3Path release];
}
NSLog(@"frlisturls2 is%@:",frlisturls2);
[super viewDidLoad];
fileName = [[NSString alloc]init];
fileName=[frlisturls2 objectAtIndex:iRow ];
NSLog(@"the fileName is %@",fileName);
stringSources = [NSString stringWithContentsOfFile: fileName encoding:NSUTF8StringEncoding error:nil];
frtextview=[[UITextView alloc]init];
[frtextview setText:stringSources];
NSLog(@"the file is :%@",stringSources);

文件名显示正确的网址,但字符串源没有文字。很抱歉这么长的帖子!希望对您有所帮助!

最佳答案

这就是我将txt文件读取到txtView的方式

但有时文件无法用文本编辑器打开,

NSString *filePath = @"Users/macbookpro/Library/Application
Support/iPhone Simulator/4.3.2/Applications/ ...
/Documents/sample.txt";
NSString *txtContent = [[NSString alloc]
initWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:&error];

将 txtContent 加载到您的 textView.text

关于ios - 在 UITextView 中显示 txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7916073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com