开发者

Reading a text file from desktop at app startup

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜