在应用启动时从桌面读取文本文件

Pau*_*aul 4 macos cocoa objective-c text-files nstextview

我需要在启动时显示位于用户桌面上的文本文件的内容NSTextView.我的代码不起作用 - 是否偏离轨道?

NSError *err = nil;

NSString *filepath = @"~/Desktop/test.txt";

NSString *file = [NSString stringWithContentsOfFile:filepath 
                                           encoding:NSUTF8StringEncoding 
                                              error:&err];

if(!file) {

}

[textView setString:file];
Run Code Online (Sandbox Code Playgroud)

Pau*_*aul 7

@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];
Run Code Online (Sandbox Code Playgroud)