1

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?

Claudiu
  • 224,032
  • 165
  • 485
  • 680

1 Answers1

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