gpt4 book ai didi

iOS/objective-c : NSJSONSerialization graceful failure

转载 作者:行者123 更新时间:2023-11-28 19:42:20 25 4
gpt4 key购买 nike

当没有数据时(由于服务器问题即使有 Internet 连接也可能发生),以下崩溃。有没有一种方法可以使用错误术语使其正常降级,或者我是否必须使用另一个 if 语句,例如在调用 JSONSerialization 之前提要中是否有数据。想找到另一个 if 语句的替代方法。实际上,下面的 if 语句在代码的前面,所有的 if 都变得非常复杂。

if (internet) {
//some code
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:jsonFeed
options:kNilOptions
error:&error];
//some code
}

最佳答案

唯一的其他选择是拥有一个方法/函数来包装对 JSONObjectWithData:options:error: 的调用,并在 jsonFeed 为 nil 时返回 nil。您只是隐藏了 if 语句,但如果您在很多地方都需要它,那么这可能是值得的。

关于iOS/objective-c : NSJSONSerialization graceful failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33855387/

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