开发者

Is it OK to ignore return values?

For example, I have a factory method which sets up an UIView and adds it as subview, then returns the instance. But in some cases I don't care about what is returned. So can I call that method and just ignore what it returns?

[self addUIViewWithRect:rect];

instead of

UIView *theView = [self addUIViewWithRect:rect];

?

Yes seems to work, but this does开发者_如何学Cn't mean that it really does and also it doesn't mean that this is good. So I want to make sure.


That is fine. As long as you don't ignore non-auto released returns you are probably OK.


it depends entirely on the context, and the implementation of the method returning the value.

as Kevin stated, you'll need to make sure your ref counting is correct.

in objc, returning a value vs nil can often be used to indicate an error. it's a good idea to check for such error conditions, and verify the program works as it should. in this case, your superview may have implemented some logic to detect errors, returning nil may be used to indicate that an error was encountered, and that the view was not added. whether you can ignore that or should not ignore it depends on the context of the program.

if you choose to ignore it, you'll have to take each on a case-by-case basis, and consider the context of the program.

but it's not common to ignore them in many cases, take this common (C) example:

printf("stuff"); // << retval ignored
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜