开发者

Reading a text file from desktop at app startup

开发者 https://www.devze.com 2023-03-10 05:56 出处:网络
I need to display the contents of a text file located on the user\'s desktop in an NSTextView at startup . My code is not working -- is it off track?

I need to display the contents of a text file located on the user's desktop in an NSTextView at startup . My code is not working -- is it off track?

NSError *err = nil;

NSString *filepath = @"~/Desktop/test.txt"开发者_JAVA技巧;

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

if(!file) {

}

[textView setString:file];


@Shem. Sorry about that.

I fixed it like this:

 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];


Your path to the text file looks off ... possibly: /Users/USERNAME/Desktop/test.txt Also, try NSLog(@"Error: %@",[err description]); to see what errors are generated.

0

精彩评论

暂无评论...
验证码 换一张
取 消