本文介绍了如果NSString stringWithContentsOfFile不推荐,它的替代是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些来自Tuaw的示例代码,可能是3个旧版本;)编译器发出警告,表示该方法已被弃用,但我在SDK文档中没有提到。如果不推荐使用,则必须有替代方法或替换方法。
I have some sample code from Tuaw which is probably 3 releases old ;) The compiler is issuing a warning that the method is deprecated, but I do not see that mentioned in the SDK docs. If it is deprecated, there must be an alternative approach or replacement method. Does anyone know what the replacement is for this method?
具体代码是:
NSArray *crayons = [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"crayons" ofType:@"txt"]] componentsSeparatedByString:@"\n"];
修改后的代码(在离散的白痴步骤中,没有错误处理)是:
The modified code (in discrete idiot steps - and no error handling) is:
NSError *error;
NSString *qs = [[NSBundle mainBundle] pathForResource: @"crayons" ofType: @"txt"];
NSString *ps = [[NSString alloc] stringWithContentsOfFile:qs encoding:NSUTF8StringEncoding error: &error];
NSArray *crayons = [[NSArray alloc] arrayWithContentsOfFile: ps];
推荐答案
使用:
+ (id)stringWithContentsOfFile:(NSString *)path
usedEncoding:(NSStringEncoding *)enc
error:(NSError **)error
请查看了解更多信息。
这篇关于如果NSString stringWithContentsOfFile不推荐,它的替代是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!