Objective c 在应用程序启动时从桌面读取文本文件
我需要在启动时在Objective c 在应用程序启动时从桌面读取文本文件,objective-c,cocoa,macos,text-files,nstextview,Objective C,Cocoa,Macos,Text Files,Nstextview,我需要在启动时在NSTextView中显示位于用户桌面上的文本文件的内容。我的代码不起作用了——它偏离轨道了吗 NSError *err = nil; NSString *filepath = @"~/Desktop/test.txt"; NSString *file = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEn
NSTextView
中显示位于用户桌面上的文本文件的内容。我的代码不起作用了——它偏离轨道了吗
NSError *err = nil;
NSString *filepath = @"~/Desktop/test.txt";
NSString *file = [NSString stringWithContentsOfFile:filepath
encoding:NSUTF8StringEncoding
error:&err];
if(!file) {
}
[textView setString:file];
您的文本文件路径看起来不正确。。。可能:
/Users/USERNAME/Desktop/test.txt
另外,请尝试NSLog(@“Error:%@,[err description])编码>
以查看生成了哪些错误。@Shem。很抱歉
我是这样修复的:
NSError *err = nil;
NSString *filepath = @"~/Desktop/test.txt";
filepath = [filepath stringByExpandingTildeInPath];
NSString *file = [NSString stringWithContentsOfFile:filepath
encoding:NSUTF8StringEncoding
error:&err];
if(!file) {
}
[textView setString:file];
我明白了;)忘记:filepath=[filepath stringByExpandingTildeInPath]谢谢您应该将其作为答案发布。它应该作为答案发布,但新的用户限制可能会阻止这种情况发生,直到明天@Peter~是用户主目录的缩写,通常位于“/Users/USERNAME”。您建议提问者尝试访问/Users/USERNAME/Users/USERNAME/Desktop/test.txt,这是不正确的。@Paul-为什么您接受了不正确的答案?给它一点时间——没有规则说你必须接受第一个发布的答案。