iOS - 如何将html资源文件的整个内容读入NSString*?

Suc*_*chi 11 iphone ios

我想将html资源文件的内容放入NSString对象中.这样做是否可行且可取?怎么可能呢?

Kri*_*izz 28

可能? - 是的

可取? - 除非它是一个非常大的文件,为什么不呢?

怎么样?-已经有这样做对你的方法NSString- stringWithContentsOfFile:encoding:error:.

请参阅下面的代码:

NSError* error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource: @"foo" ofType: @"html"];
NSString *res = [NSString stringWithContentsOfFile: path encoding:NSUTF8StringEncoding error: &error];
Run Code Online (Sandbox Code Playgroud)