我有一些来自 Tuaw 的示例代码,可能是 3 个版本的旧版本;)编译器发出警告,表明该方法已被弃用,但我没有看到 SDK 文档中提到这一点。如果不推荐使用,则必须有替代方法或替代方法。有谁知道这个方法的替代品是什么?
具体代码是:
NSArray *crayons = [[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"crayons" ofType:@"txt"]] componentsSeparatedByString:@"\n"];
修改后的代码(在离散的白痴步骤中 - 并且没有错误处理)是:
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
享受!查看文档 http://developer.apple.com/mac/library/documentation/cocoa/reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithContentsOfFile:usedEncoding:error:了解更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)