如何通过间接传递(和设置)非对象?
NSError 对象经常像这样使用(取自 这之前问题):
- (id)doStuff:(id)withAnotherObjc error:(NSError **)error;
我想通过 BOOL 间接实现类似的目标:
- (id)doStuff:(id)withAnotherObjc andExtraBoolResult:(BOOL **)extraBool;
但我不知道如何让它正常工作。
对于涉及 NSError 的给定方法规范,正确的实现将涉及类似的内容(同样来自 上一个问题):
*error = [NSError errorWithDomain:...];
通过类似的逻辑,似乎这应该适用于 BOOL 间接:
*extraBool = &YES; // ERROR! Address expression must be an lvalue or a function designator
为什么这不起作用以及实现此目的的正确方法是什么?
NSError objects are frequently used like this (taken from this previous question):
- (id)doStuff:(id)withAnotherObjc error:(NSError **)error;
I want to achieve something similar with BOOL indirection:
- (id)doStuff:(id)withAnotherObjc andExtraBoolResult:(BOOL **)extraBool;
But I can't figure out how to get this working correctly.
For the given method specification involving NSError, the proper implementation would involve something like (again from the previous question):
*error = [NSError errorWithDomain:...];
With similar logic, it seems like this should work with BOOL indirection:
*extraBool = &YES; // ERROR! Address expression must be an lvalue or a function designator
Why doesn't this work and what is the proper way to implement this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,对于对象,您正在使用指针(例如,
NSError*
),因此使用此方法,您最终会得到一个指向指针的指针(例如,NSError* *)。但是,在使用
BOOL
时,您应该使用指向 BOOL 的指针:也就是说,只有一层间接寻址,而不是两层。因此,您的意思是:然后:
Keep in mind that with objects, you're working with a pointer (e.g.,
NSError*
), so using this method, you wind up with a pointer to a pointer (e.g.,NSError**
). When working with aBOOL
, though, you should use a pointer to a BOOL: that is, only one level of indirection, not two. Therefore, you mean:and subsequently: