我想知道我可以使用什么代码来代替initWithContentsOfFile:因为我一直在搜索一些没有被弃用的东西,但找不到任何东西。我试图在webview中显示一个本地HTML文件,以下是代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *html = [[NSString alloc] initWithContentsOfFile:htmlPath];
[self.myWebView loadHTMLString:html baseURL:bundleUrl];
}我收到一个警告,说initWithContentsOfFile:已被弃用,我想知道如何更新它。
发布于 2012-01-24 17:19:38
试试这个:
NSError *error = nil;
NSString *string = [[NSString alloc] initWithContentsOfFile:@"/path/to/file.txt" encoding:NSUTF8StringEncoding error:&error];发布于 2012-01-24 17:19:14
使用较新的初始值设定项:
NSString的[initWithContentsOfFile: encoding: error:]方法,如果有任何问题,你可以得到一个有用的错误回传给你。我已经为您链接了文档。如果不知道文件的编码,也可以使用具有usedEncoding参数的另一种方法。
https://stackoverflow.com/questions/8984308
复制相似问题