可以重用同一个NSError对象吗?

编程入门 行业动态 更新时间:2024-10-25 12:26:57
本文介绍了可以重用同一个NSError对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

此后,我将实现更改为不再,但是我想知道以下内容是否会导致问题-例如,一个过程包含多个步骤,每个步骤都涉及通过对NSError对象的引用.如果您要在每个步骤中重用此错误对象,该引用是否会受到某种负面影响,也许会导致某种内存管理/dealloc崩溃?

I have since changed my implementation to not do this anymore, but I would be interested in knowing if the following could lead to issues - take for example a process that has multiple steps, each involving passing a reference to an NSError object. If you were to reuse this error object each step along the way, would that reference be negatively effected in some way, perhaps leading to some sort of memory management/dealloc crash?

NSError *error; NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error]; if (!data) { return error; } NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if (!responseData) { return error; } NSDictionary *resultDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error]; if (!resultDictionary) { return error; }

即使该错误从未被使用过"两次,这意味着该错误在下一次将其引用传递给另一种方法时将为零(看来),该引用是否可以在构建过程的任何步骤中以某种方式实现方式吗?

Even though the error is never "used" twice, meaning the error will be nil (it seems) the next time its reference is passed to another method, could the reference be effected in some way at any of the steps along the way?

根据jlehr的评论进行编辑之前的原始代码:

Original code prior to editing in response to jlehr's comments:

NSError *error; NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error]; if (error) { return; } NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if (error) { return; } NSDictionary *resultDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error]; if (error) { return; }

对于以后再来的人,jlehr所指的信息可以在这里找到:

To anyone coming to this at a later date, the information jlehr is referring to can be found here:

在处理通过引用传递的错误时,进行测试很重要 该方法的返回值,以查看是否发生错误,如 如上所示.不要只是测试看看是否设置了错误指针 指出错误.

When dealing with errors passed by reference, it’s important to test the return value of the method to see whether an error occurred, as shown above. Don’t just test to see whether the error pointer was set to point to an error.

developer.apple. com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/ErrorHandling/ErrorHandling.html

推荐答案

根据Apple的文档,永远不要检查引用返回的NSError对象.相反,请检查要调用的init...方法或工厂方法的返回值,例如:

According to Apple's documentation, you should never check an NSError object returned by reference. Instead, check the return value of the init... method or factory method you're calling, for example:

NSError *error; NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error]; if (data == nil) { // Handle failure. It's safe to use the error object here. }

但是,您的代码似乎没有对NSError实例执行任何操作(例如,记录日志或显示警报),因此您只需传递NULL即可.

However, your code doesn't appear to be doing anything with the NSError instance, (e.g., logging, or displaying an alert), so you can just pass NULL instead.

NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:NULL]; if (data == nil) { // Handle failure. }

更多推荐

可以重用同一个NSError对象吗?

本文发布于:2023-11-22 08:27:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616687.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   NSError

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!