开发者

Objective c: NSString NSData reading from file problem

I'm using:

NSData *output1 = [NSData dataWithContentsOfFil开发者_Go百科e:@"~/centralUtilOut.tmp"];
NSString *output = [[NSString alloc]initWithData:output1 encoding:NSUTF8StringEncoding];

NSLog(@"%@", output);
[output release];

But nothing is in the debug window.

This is in objective C.

Note: centralUtilOut.tmp is a normal text file


The problem is in the path specification.

It seems that the NSData -dataWithContentsOfFile: does not expand ~.

It works when you use full path or expand tilde in path:

NSData *output1 = [NSData dataWithContentsOfFile:
                      [@"~/centralUtilOut.tmp" stringByExpandingTildeInPath]];
NSString *output = [[NSString alloc]initWithData:output1 
                                        encoding:NSUTF8StringEncoding];

NSLog(@"%@", output);
[output release];


That tilde in the path makes me think your file path might not be getting handled properly. Take a look at NSString's -stringByExpandingTildeInPath method to expand the path to the full, absolute path.

For example: NSData *output1 = [NSData dataWithContentsOfFile:[@"~/centralUtilOut.tmp" stringByExpandingTildeInPath]];

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜