I want to return nil
from a function that expects to get id
. The compiler complains about this when I'm writing blocks. So, is it ok to return (id)nil;
? Or am I misunderstanding something about what nil
and id
are?
Asked
Active
Viewed 96 times
1

Claudiu
- 224,032
- 165
- 485
- 680
-
What does the compiler say? – jscs Sep 13 '13 at 00:57
-
@JoshCaswell: it says "Incompatible block pointer types sending `'void *(^)(void)'` to parameter of type `'id (^)()'`. – Claudiu Sep 13 '13 at 01:00
1 Answers
4
It is valid. nil
"should" be type of id
(actually it is void *
for some reason) but it is totally safe to cast nil
to id
. Similarly apply to 0
and NULL
(they all just 0
). And send message to 0
result defined behaviour (nop) so everything is fine.
I have the same problem about return nil
in block and this is quote from the answer
:
You're using an inferred return type for your block literal, but you don't have to.
The correct way to remove that error is to provide a return type for the block literal:
id (^block)(void) = ^id{
return nil;
};

Community
- 1
- 1

Bryan Chen
- 45,816
- 18
- 112
- 143
-
how do I specify the return type when I'm providing the block in-line to another function call? – Claudiu Sep 13 '13 at 01:00
-
3