gpt4 book ai didi

iphone - 如果 NSString stringWithContentsOfFile 已被弃用,那么它的替代品是什么?

转载 作者:行者123 更新时间:2023-12-03 16:03:23 26 4
gpt4 key购买 nike

我有一些来自 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

享受吧!查看documentation了解更多信息。

关于iphone - 如果 NSString stringWithContentsOfFile 已被弃用,那么它的替代品是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2954892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com