同步返回objective-c
- (id)methodThatReturnsSomething { @synchronized(self) { return nil; } }
当我在 Xcode 上执行此操作时,它会返回一条警告:“控制到达非 void 函数的末尾”
该代码有任何问题吗?
- (id)methodThatReturnsSomething { @synchronized(self) { return nil; } }
When I do this on Xcode, it returns me a warning: "Control reaches end of non-void function"
Is there any problem with that code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它会发出编译器警告,因为某些版本的编译器中存在错误,而这些错误已在其他/更高版本的编译器中修复。
在这种情况下,是的,您确实偶然发现了编译器错误。
It barfs up a compiler warning because of a bug in some versions of the compiler that are fixed in other / later versions of the compiler.
In this case, yes, you really did stumble upon a compiler bug.
发布的代码中的同步是多余的,但没有问题:
@synchronized
块要么正常退出,要么通过异常退出。由于其中已经有一个return
语句,因此不需要在该块之后再添加一个语句。The synchronization in the code as posted is redundant but there is no problem with it as such:
@synchronized
blocks are either exited normally or through exceptions. As you already have areturn
statement in it, another statement after the block is not needed.我不明白您想在代码中做什么,但
应该具有相同的效果(推迟返回,直到释放与
self
关联的锁为止),而不会出现编译器警告。但是:你想做什么?您不必以这种方式放置
@synchronized
。I don't understand what you want to do in your code, but
should have the same effect (postponing the return until the lock associated to
self
is freed), without the compiler warning.But: what did you want to do? You don't have to put a
@synchronized
in this manner.