- (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?
- (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?
I don't understand what you want to do in your code, but
- (id)methodThatReturnsSomething
{
@synchronized(self) {
}
return nil;
}
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.
Yes,
The @synchronized block is similar to doing this (my terrible pseudocode)
- (id)methodThatReturnsSomething
{
if ([self notlocked]) {
[self lock]
return nil;
[self unlock]
}
}
You can see that if another function call is made, then it will reach the last } without returning something.
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 a return statement in it, another statement after the block is not needed.
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.